10 important javaScript concepts for interviews

Mijan Karim
3 min readMay 8, 2021

Truthy vs Falsy values

Every value in javascript is either truthy or falsy.
The following six values are considered falsy values in JavaScript.

false, undefined, null, "" , NaN , 0

Every other value is considered truthy.

Undefined vs Null

Undefined means a variable that has been declared but no value has been assign to the variable yet.

let name;
console.log(name); //output undefined

Null means a variable has no value. Null has to be assigned.

let age = null
console.log(age); //output null

Both undefined and null are falsy values.

Triple equal vs Double equal (=== vs ==)

Triple equal === in javaScript check both type and value which is called strict equality. On the other side Double equal == check value only which is called loose equality.

2 === '2' // false because Number vs String 
2 == '2' // true

var, let, and const

Var declared variables are function scoped. Scope means where the variable is available in the code.

function person(){
var name = "Joseph";
console.log(name); //output Joseph
}
person();
console.log(name); // name is not defined

Other block types like if-else and loop will not be scope.

if(true){
var name = "Joseph";
console.log(name); //output Joseph
}
console.log(name); //output Joseph

var name can be accessed outside of the block. Because they exist in the same block.

let and cont both are block-scoped. so both will be scoped on any type of block-like function, if-else, loop, etc.

function person(){
let name = "Joseph";
console.log(name); //output Joseph
}
person();
console.log(name); // name is not defined
if(true){
let name = "Joseph";
console.log(name); //output Joseph
}
console.log(name); //name is not defined

call(), apply() and bind()

call() method calls a function with the given value and other arguments provided in the function.

const person = {name: 'Joseph', age:32};
function profile(city, country){
console.log('Name: ' + this.name+', Age:'+ this.age + ', City: ' + city + ', Country: ' + country);
}
profile.call(person,'Chattogram','Bangladesh');
//Name: Joseph, Age:32,City:Chattogram, Country: Bangladesh

apply() method is similar to call() method except it allows you to pass the arguments as an array.

const person = {name: 'Joseph', age:32};
function profile(city, country){
console.log('Name: ' + this.name+', Age:'+ this.age + ', City: ' + city + ', Country: ' + country);
}
profile.apply(person,['Chattogram','Bangladesh']);
//Name: Joseph, Age:32,City:Chattogram, Country: Bangladesh

bind() method returns a new function with the value bound to it. That function can be called with required arguments.

const person = {name: 'Joseph', age:32};
function profile(city, country){
console.log('Name: ' + this.name+', Age:'+ this.age + ', City: ' + city + ', Country: ' + country);
}
const info= profile.bind(person);
info('Chattogram','Bangladesh');
//Name: Joseph, Age:32,City:Chattogram, Country: Bangladesh

this keyword

this keyword in javaScript indicates the object it belongs to. In a function, this indicates the global window object.

var person = {
firstName: "John",
lastName : "Doe",
fullName : function() {
return this.firstName + " " + this.lastName;
}
};

In this example, this refers to the person object.

setTimeout and setInterval

setTimeout is used to run a function once after a certain period of time.

setTimeout(()=> console.log('Hello'), 1000);

setInterval is used to run a function repeatedly after a certain period of time, then continuously repeat at that time interval.

setInterval(()=> console.log('Hello'), 1000);

Closure

A closure is a function that has access to its outer function scope even after the outer function has returned. Closer has access to its own scope, its outer functions scope, and the global scope.

var a = 10;
function outer(){
var b = 5;
var c = 8;
function inner(){
var sum = a + b;
console.log(sum); //15
}
inner();
}
outer();

Callback function

In javascript, a function can be passed as parameters to other functions and call it back right after some task completed. It is called the call back function.

function doSomething(callback){
console.log('It does something');
callback();
}
function done(){
console.log('Task has been done');
}
doSomething(done);
// It does something
// Task has been done

“New” keyword

“New” keyword creates a new object and binds it to “this” keyword.

function Person(name,age) {
this.name = name;
this.age = age;
}
var singer = new Person('Asif',50);
console.log(singer.name); //Asif

--

--