Common JavaScript Interview Question That You Need to Know

In this lesson, we are quickly going to know some common javascript interview questions that every javascript developer should know. let’s start,

What Is JavaScript?

What are Truthy and Falsy Values in JavaScript?

  • -1,10 (or other negative and non-zero numbers)
  • {} (empty objects)
  • [] (empty arrays)
  • function () {}, () => {} (functions, including empty functions)
  • non-empty strings (like “false”, ‘ ‘ — including strings with just whitespace)

Other values are considered falsy values. a value that evaluates to the boolean value false expressions, statements, and values can evaluate to the false boolean value and will therefore be considered a falsy value. Some falsy values to remember:

  • null
  • undefined
  • NaN
  • false
  • 0 (or other forms of the number 0, -0, 0.0 )
  • empty strings ('', "", etc)

What is the difference between null & undefined?

What do you understand by NaN?

What is the difference between the operators ‘==‘ & ‘===‘?

Explain Scope in javascript?

There are three types of scopes in JavaScript:

  • Global Scope
  • Local or Function Scope
  • Block Scope

Global Scope

var global = "Hello Global Scope";function globalMessage(){
return global; // can access global Variable since it's written in global space
}

function globalMessage2(){
return globalMessage(); // Can access globalMessage function since it's written in global space
}

globalMessage2(); // Returns Hello Global Scope

Function Scope

function functionScope(){
var a = 2;

var multiplyBy2 = function(){
console.log(a*2); // Can access variable "a" since a and multiplyBy2 both are written inside the same function
}
}
console.log(a); // Throws reference error since a is written in local scope and cannot be accessed outside

multiplyBy2(); // Throws reference error since multiplyBy2 is written in local scope

Block Scope

{
let a = 12;
}

console.log(a); // Gives reference error since a cannot be accessed outside of the block

for(let i=0; i<5; i++){
// do something
}

console.log(i); // Gives reference error since i cannot be accessed outside of the for loop block

Scope Chain

var a = 32;

function functionScope(){
var b= 16;
var scopeChain = function(){
console.log(b); // Does not find b inside scopeChain Function, so looks for variable inside favFunction, outputs 16
}

var scopeChain2 = function(){
console.log(a); // Does not find a inside scopeChain2 Function, so looks for variable inside favFunction and does not find it, so looks for variable in global scope, finds it and outputs 32
}

scopeChain();
scopeChain2();
}


functionScope();

What are closures in JavaScript?

  1. Own scope where variables defined between its curly brackets
  2. Outer function’s variables
  3. Global variables

Let’s take an example of the closure concept,

function closure(str){
const message = function(str2){
console.log(str + ' ' + str2);
}
return message;
}
const closureFunction = closure('I am Closure');
closureFunction('hello '); //Output: hello I am Closure
closureFunction('hi.'); //output: hi I am Closure

Explain bind(), call() and apply() in JavaScript?

bind()

const normalPerson = {   firstName: 'Rahim',   lastName: 'uddin ',   salary: 15000,   chargeBill: function(amount){      this.salary = this.salary - amount;      return this.salary;   }}const heroPerson = {    Name: 'Hero',    lastName: 'Balam',    salary: 25000}
const heroBillCharge = normalPerson.chargeBill.bind(heroPerson);
heroBillCharge(2000)console.log(heroPerson.salary); // 23000 ( 25000 - 2000 )

call() and apply()

The basic difference between .call() and .apply() is in the way arguments are passed to the function. Their usage can be illustrated by the given example.

const normalPerson = {     firstName: 'Rahim',     lastName: 'uddin ',     salary: 15000,     chargeBill: function(amount, tax){        this.salary = this.salary - amount - tax;        return this.salary;     }}const heroPerson = {     Name: 'Hero',     lastName: 'Balam',     salary: 25000}// .call()normalPerson.chargeBill.call(heroPerson, 1000, 50);console.log(heroPerson.salary); // 2350 // .bind()normalPerson.chargeBill.apply(heroPerson,[30000,1000])console.log(heroPerson.salary); // 21000

What is the purpose of the ‘This’ Keyword in JavaScript?

const check= {
prop: 18,
func: function() {
return this.prop;
},
};
console.log(check.func());
// expected output: 18

Explain event loop in JavaScript?

The call stack is responsible for keeping track of all the operations in line to be executed. Whenever a function is finished, it is popped from the stack.

The event queue is responsible for sending new functions to the track for processing. It follows the queue data structure to maintain the correct sequence in which all operations should be sent for execution.

Whenever an async function is called, it is sent to a browser API. These are APIs built into the browser. Based on the command received from the call stack, the API starts its own single-threaded operation.