JavaScript InterView Question all you need to know

Imnulhaqueruman
3 min readMay 8, 2021

Truthy and Falsy

const num = 2; the condition is true for all positive and negative number without 0.

const name =” EMon “; the condition is true when name variable assign any string or space but it's false when it assigns an empty string

Falsy

0 , null, NaN,undefined, “”

Truthy

“0” “ “ [] {}

const name1 = ``;

if(name1){ console.log(‘condition is true’);}

else{ console.log(‘condition is False’);;}

Null vs Undefined

When we write code we get undefined various way …

  1. we will get undefined if we declare a variable but don’t assign any value .
let num;
console.log(num)//undefined

2. If we decalre a function without return any value that will get undefined when we called this function and if we don’t assign any value of parameter .

function add(num1, num2){
console.log(num1+num1)
}
const result = add(32,35);
const result = add)(32);
console.log(result)//undefined

3. We will get undefined when we read any object property and array value that is not contain in object and array .

let freinds = {name:'Emon', age:'21'}
console.log(friends.phone)// undefined

Null

we set explicitly null where doesn't any value .

=== vs ==

Double equals compare two values after converting both values in common type .

for example :

let first = 2;
let second = '2';
if(first == second){
console.log('condition is true')
}
else{
console.log('condition is false')}
//condition is true
  • undefined and null are loosely equal; that is, undefined == null is true, and null == undefined is true

Third equal check value and type when value and types are equal both of values then it’s true.If the values have different types, the values are considered unequal. If the values have the same type, are not numbers, and have the same value, they’re considered equal

var num = 0;
var obj = new String('0');
var str = '0';

console.log(num === num); // true
console.log(obj === obj); // true
console.log(str === str); // true

Scope

The current context of execution . A function creates a scope in JavaScript ,so that a variable declared in function it cannot access from outside .

function add(num1, num2){
let result = num1+num2;
return result;
}
const sum = add(3,4)
console.log(sum);
console.log(result)// error

Block Scope

Block Scope is defined by pairs of curly brackets.In curly brackets we decalre any variable it creates block scope that means we can not access this variable out side of scope .When we declare a veraivle with const, let key in curly brackets specially that creates block scope .Variables declared with var do not have block scope .

function add(num1, num2){
let result = num1+num2;
return result;
if(result >9){
const mood ='happy';
console.log(mood)//happy
}
const sum = add(6,4)

console.log(mood)// error

Closure

Closure means that has an inner function which always access vars and parameters of outer function and return inner function.

function outerFunction(){
let num=100;
return function innerFunciton(){
num++;
return num
}
}
const myFunc = outerFunction();
myFunc();

when we call outerFunction with veraible myFunc return innerFunction.

when you call innerFunc(), it can still access outerVariable which is declared in OuterFunction(). This is called Closure.

Global Variable

Global Variable is a variable that is visible from all other scopes.

var name='sum';
function add(num1, num2){
var result = num1 + num2;
console.log(name);
return result;
}
const sum = add(4,5);
console.log(sum)//9
console.log(result)//error

In above exapmle result variable decalred in function scope but when we call it outside of function then get error, name variable is global variable because we can access it outisde and innerside.

new Keyword

In JavaScript when we work with object than we make many type of object that template will same so for this reason we create new object from class with new keyword.

class Person {
constructor(firstName,lastName,salary){
this.firstName=firstName;
this.lastName=lastName;
this.salary=salary;
}
}
const newObjcet = new Person('Imnul','Ruman', 3000)
console.log(newObject)

--

--