Most common javascript interview question.

Nurul Alam
3 min readMay 15, 2021

Null Vs Undefined

In javascript Null and Undefined little bit confusing. So, today I will clear the thing. In javascript null means the variable value has empty or you set the value null. When you declare a variable, if the variable value is empty It will show null. In javascript undefined means, you declare a variable but you do not the set value or pass the value, It will show undefined.

const checkValue;
console.log(checkValue)// undefiend
const fun = (num1, num2) => {
console.log(num2)};
const result = fun(25)
console.log(result)// undefined
const text = undefiend;
console.log(text)// undefined
const isNull = null
console.log(isNull) // null

Note: null is only used when You expected you get an empty value or get a zero.

Double equal vs Triple equal

When you use a double equal in a condition its only check the value. If the value is equal (equivalent)it will show true. On the other hand Triple equal in a condition check the value and check the type. if value or type is not equal then it is thrown false.

//double equal
const num = '5';
if(num == 5){
console.log(true)
} else {
console.log(false)
};
//true
//triple equal
const num = '5;
if(num === 5){
console.log(true)
} else {
console.log(false)
};
// false

Note: It is recommended to use triple equal to reduce bug in your code.

Scope and block scope

When you declare a variable Its create a scope. And It is the global scope. You can access this variable in any function. But if you declare a variable in a curly brace {} or a function, It creates a block scope. You can only access this variable in the block scope. let's have an example.

const str = 'cat';
const func = ()=> {
console.log(str)
}
console.log(str)
// cat
{ const animal = 'dog' }
console.log(animal)
// animal is not defined

This keyword

In javascript, this keyword confuses a lot of developers. This keyword references the objects that are executing the current function. If the function is the method in an object this references that object itself. Otherwise, if the function is a regular function, which means it's not a part of an object, This references global objects.

//this in the object
const animals = {
name: 'cat',
fun(){
console.log(this);
}
};
animals.fun();
// {name: "cat", fun: ƒ}
//this in the regular funtion
function fun2(){
console.log(this);
}
fun2();
// Window {window: Window, self: Window, document: document, name: "", location: Location, …}

In this example we can see this keyword in a regular function it references global objects. But in strict mode this keyword reference undefined. What if we using a constructor function instead of a regular function. And call this function with a new keyword.

function Fun2(name) {
this.name = name;
console.log(this);
};
const fun = new Fun2('dog');
// Fun2 {name: "dog"}

Let’s recap by dealing with a regular function this keyword reference global object. But in a constructor function this reference new objects.

Closure

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.

function add() {
let increase = 0;
return function () {
increase++;
return increase;
};
}
const makeAdder1 = add();
console.log(makeAdder1()); // 1
console.log(makeAdder1()); //2
const makeAdder2 = add()
console.log(makeAdder2()) //1

In this example, we create an add function inside this add function we return another function. This function increases the value. Note that the inside function() has no local variables of its own. However, since inner functions have access to the variables of outer functions, the inside function can access the variable increase declared in the parent function, add().

--

--

Nurul Alam
0 Followers

An enthusiast JavaScript and React developer.