Some JavaScript Interview Questions

Tanjin Ahamed
4 min readMay 8, 2021

null vs undefined

Q: Could you tell me the differences between null and undefined?

Ans:-usually we can make 7 to 8 ways. the differences are given below

  1. let maria;//no value has been set.

console.log(maria);

output: undefined//because there is no value has been set

2. function add(num1,num2){

console.log(num1,num2);//there is nothing returned

}

const result =add(10,2);

console.log(result);

output: undefined

if a function do not return so that result would be undefined, again if you write return but you did not mention what you have returned the result would be undefined. here is example

function add(num1,num2){

console.log(num1,num2);

return;//

}

const result =add(10,2);

console.log(result);

output: undefined

3.function add(num1,num2){

console.log(num2);

}

const result =add(2);

console.log(result);

output: undefined//because we pass num1 but we did not pass num2 that’s why the result is undefined.

4.const shimu={age:25,phone:123456789}

console.log(maria.address)

output:undefined//because address property is not set.

null:

usually null does not set, pass. null is nothing. It is supposed to be something that doesn’t exist.

Truthy & Falsy

Truthy & Falsy values are coerced to Booleans when in statements. You can use these statements in condition block in switch, if-else, while blocks. You will see this future more & more. So just read now for easier future 😎

‘’ or “” (Empty String) , false , 0 , NaN, undefined, null are falsy values.

& rest are truthy values. These are false too 😳

console.log({} == {}) console.log([] == [])

Double Equal vs Threepol Equal

There have two kinds of quality check operators Loose & Strict Equality Operator. == check operands value & apply automatically coercion, it doesn’t check operands type.

strict equality === operator checks operands value & type. Doesn’t apply coercion.

// loose equality vs strict equality
"55" == 55 // -> true
"55" === 55 // -> falseconsole.log("2" == 2) // true
console.log ("2.04" == 2.04) // true
console.log(true == 1) //true
console.log("0" == false) //trueconsole.log(2 === 2) // true
console.log("Hello" === "Hello") // true
console.log ("2.04" === 2.04) // false
console.log(true === 1) //false

Scope

Scope determines the accessibility (visibility) of variables.

JavaScript Function Scope

In JavaScript there are two types of scope:

  • Local scope
  • Global scope

JavaScript has function scope: Each function creates a new scope.

Scope determines the accessibility (visibility) of these variables.

Variables defined inside a function are not accessible (visible) from outside the function.

call apply and bind

Could you tell me the difference between call apply and bind?

if any method in the object and if we want to apply that method to another object then we can use call, apply, and bind.

Call: call is just to call that method. after calling as the first argument you want to apply that object then you will pass so that the remaining parameters you have that parameter will send by comma separation.

Apply: apply for works the same. just want to use another object the method will apply that method. as the first argument (this) gives. but which you are using that method has those parameters. Those parameters will send within that array.

Bind: bind is if you want to the same method again and again then you will take that method and binding.

here is an example bind call and apply you can see.

Local JavaScript Variables

Variables declared within a JavaScript function, become LOCAL to the function.

Local variables have Function scope: They can only be accessed from within the function.

// code here can NOT use carNamefunction getCarname() {
var carName = "Lamborgini";

// code here (inside function) CAN use carName
}// code here can NOT use carName

Since local variables are only recognized inside their functions, variables with the same name can be used in different functions.

Local variables are created when a function starts and deleted when the function is completed.

Global JavaScript Variables

A variable declared outside a function becomes GLOBAL.

A global variable has the global scope: All scripts and functions on a web page can access it.

Automatically Global Variable

If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable.

This code example will declare a global variable, even if the value is assigned inside a function.

myFunction();

// code here can use carName

function myFunction() {
carName = "Volvo";
}

Strict Mode

in the mid-level project, if someone creates a global variable that may conflict with others code, which can be the reason for some unexpected errors. Stop automatically creating global variables by using 'use strict' lines at the top of code blocks.

NaN

NaN is a property that denotes a result that is not a number. It is a property of the global object. In other words, it is a variable in the global scope. The isNaN() is a function that determines whether a value is an illegal number or not. When the function finds a value of that condition it returns true, false otherwise.

cookie is read in JS

A cookie is a small data that stored in browser storage by which it stores previous seasons data of a website or user’s information after the system shut down also. It is used to authenticate and auto remembers user names, passwords, and other information of a particular user. In javascript, we can set and get cookies by including certain functions in our program.

--

--