Interview Bit Questions


What is Hoisting in JS?

Hoisting is a behaviour in JavaScript where variable and function declarations are moved to the top of their respective scopes during the compile phase. This means you can use variables and functions before they are declared in the code. However, only the declarations are hoisted, not the initialisations.

console.log(a); // undefined (var is hoisted, but uninitialized)
var a = 10;

console.log(b); // ReferenceError (let is hoisted but in TDZ)
let b = 20;

foo(); // Works fine (function declaration is hoisted)
function foo() {
  console.log("Hello");
}

bar(); // TypeError (function expression is hoisted as undefined)
var bar = function() {
  console.log("Hi");
};

Difference between “ == “ and “ === “ operators.

Both are comparison operators. The difference between both the operators is that “==” ( loose equality ) is used to compare values whereas, “ === “ ( strong equality ) is used to compare both values and types.

Explain Implicit Type Coercion in JavaScript.

Implicit type coercion in JavaScript is the process where JavaScript automatically converts one data type into another when performing operations involving different types. This usually happens when using operators like +, -, ==, etc., on operands of different types.