DEV Community

Cover image for 10 Important JavaScript Interview Questions
Habibullah Bahar Piash
Habibullah Bahar Piash

Posted on

10 Important JavaScript Interview Questions

I am writing here about 10 important JavaScript interview questions.

01. Do you know About truthy and falsy value?

As a beginner, it will confuse you which things are truthy and which things are falsy.

Truthy: All defined values are truthy value(except forfalse, 0, -0, 0n, "", null, undefined, and NaN )

if (true){ ... }
if ({}){ ... }
if ([]){ ... }
if (42){ ... }
if ("0"){ ... }
if ("false"){ ... }
if (new Date()){ ... }
if (-42){ ... }
if (12n){ ... }
if (3.14){ ... }
if (-3.14){ ... }
if (Infinity){ ... }
if (-Infinity){ ... }
Enter fullscreen mode Exit fullscreen mode

Here will be executed the ‘if’ block.

Falsy Values: A falsy value is a value that is considered false when encountered in a Boolean context.

if (false){ ... } else { ... }
if (null){ ... } else { ... }
if (undefined){ ... } else { ... }
if (0){ ... } else { ... }
if (-0){ ... } else { ... }
if (0n){ ... } else { ... }
if (NaN){ ... } else { ... }
if (""){ ... } else { ... }
Enter fullscreen mode Exit fullscreen mode

Here will be executed the ‘else’ block.

02. Tell me the difference between double equal (==) vs triple equal (===).

Double equal check only value not type. But, triple equal always check value as well as check type.

var first = 100;
var second = '100';
if (first == second) { 
    console.log("first value & second value is eqqual")
} esle { 
    console.log("first value & second value is not eqqual")
}
// Output will be "first value & second value is eqqual"
if (first === second) { 
    console.log("first value & second value is eqqual")
} esle { 
    console.log("first value & second value is not eqqual")
}
// Output will be "first value & second value is not eqqual"
Enter fullscreen mode Exit fullscreen mode

03. Could you tell me the differences between null and undefined?

Here is few differences between null and undefined.

  1. When we create a variable without assigning any value, it will return ‘undefined’.
const name; // no value assigned to the variable
console.log(name);
Enter fullscreen mode Exit fullscreen mode
  1. If we write a function without returning anything, it will return ‘undefined’. if we write return but we did not mention what we have returned the result would be ‘undefined’.
if(5 > 2) { 
    console.log("Hello, world!");
}
// Output will be ‘undefined’
if(5 > 2) { 
    console.log("Hello, world!");
    return
}
// Output will be ‘undefined’
Enter fullscreen mode Exit fullscreen mode
  1. The value null represents the intentional absence of any object value.It is one of JavaScript’s primitive values and is treated as falsy for boolean operations. null means the variable has no value and that is known by the programmer and also value is assigned as null by the programmer.

04. Tell me something about ‘let’ vs ‘const’ vs ‘var’.

We use these keywords to create variables in JavaScript.
‘var’ : We declear variables by using ‘var’ keyword. It can be re-assign & it is a global scope variable.

var name = "Habibullah Bahar";
console.log(name); // Output: "Habibullah Bahar"
name = "Piash";
console.log(name); // Output:"Piash"
Enter fullscreen mode Exit fullscreen mode

‘let’ : It is same as ‘var’. But, it is not global scope variable. It is a block scope variable. It’s value can be re-assgin. It’s a feature of ES6.

let profession = "Web Designer";
console.log(profession); // Output: "Web Designer"
profession = "Web Developer";
console.log(profession); // Output:"Web Developer"
Enter fullscreen mode Exit fullscreen mode

‘const’ : We can also declear variables using ‘const’ keyword. But, there is restriction. It is not re-assignable & it’s scope is block scope.

const age = 20;
console.log(age); // Output: 20;
Enter fullscreen mode Exit fullscreen mode

05. ‘this’ keyword.

‘this’ keyword refers to an object, that object which is executing the current bit of javascript code. In other words, every javascript function while executing has a reference to its current execution context, called ‘this’. Execution context means here is how the function is called.

  • By default, this refers to the global object
  • In an object method, this refers to the object the method was called on
  • In a function, when JavaScript is not in strict mode, this refers to the global object
  • In a function, when in strict mode, this will be undefined
  • In an event handler, this is bound to the HTML element on which the listener is placed.

06. What is global scope in JavaScript?

Scope determines the accessibility (visibility) of variables.
When we declear a variable outside functions, it is a global scope variable. We can access the variable in any function or globally.

var name = "Piash";
function person(){
    return "My name is " + name;
}
person(); // Output: "My name is Piash"
console.log(name + " is a full stack web developer");
// Output: "Piash is a full stack web developer"
Enter fullscreen mode Exit fullscreen mode

07. What is local scope in JavaScript?

If we declared a variable inside of a function then it’s called local variable. We can access the variable inside the function. Outside the function, we can’t access the local variables.

function person(){
    var name = "Piash";
    return "My name is " + name;
}
person(); // Output: "My name is Piash";
console.log(name + " is a full stack web developer");
// Output: "Undefined is a full stack web developer"
Enter fullscreen mode Exit fullscreen mode

08. What is closure in JavaScript?

The closure is by default behavior in Javascript. If we declare a function parentFunc() and inside this function we declare another function childFunc(). When we return the parentFunc() and then from childFunc() we can also access the parent function’s variable and object also. It’s happening for Closure in Javascript.

function parentFunction(){
    var a = 8;
    function childFunction(){
        var b = 2;
        console.log(a + b); // Output: 10
   }
}
Enter fullscreen mode Exit fullscreen mode

09.How JavaScript code executed?

JavaScript program is run an execution context is created. In this phase, JavaScript allocates the memory to all the variables and functions present in the program. The variables are stored with the value undefined and the function is stored with all the code present in that particular function.

10. Could you tell me about Private variable in Javascript?

Private variable in javascript is an OOP(Object-oriented programming concepts). Which means which variable we declare as a private variable it can’t be accessible in other classes. It is only accessible in its parent class.

My Portfolio Website: https://habibullahftl.web.app/

Latest comments (0)