Most Commonly Asked JavaScript Interview Questions: Understanding JavaScript Hoisting and Scoping through Common Interview Questions

Most Commonly Asked JavaScript Interview Questions: Understanding JavaScript Hoisting and Scoping through Common Interview Questions


JavaScript interviews often include questions related to hoisting, scoping, and function declarations. Understanding how these concepts work in JavaScript can significantly improve your ability to reason through code and predict its behavior. In this article, we will explore six common interview questions that test your knowledge of these core JavaScript concepts.

Check out my detailed YouTube video on this topic here. In this video, I explain JavaScript Hoisting and Scoping with additional examples and visuals.


Question No. 1


Code:

console.log(a); 
var a = 10;        


Output:

undefined        


Explanation:

  • JavaScript hoists the declaration of var a to the top of the scope.
  • Only the declaration is hoisted, not the assignment.
  • When console.log(a) is executed, a is undefined because the assignment hasn't occurred yet.


Question No. 2


Code:

console.log(sum(2, 3));

function sum(x, y) {
    return x + y;
}        


Output:

5        


Explanation:

  • Function declarations are fully hoisted in JavaScript.
  • Both the declaration and definition of the sum are moved to the top of the scope.
  • The function sum can be called before it appears in the code, resulting in the correct output.


Question No. 3


Code:

console.log(b);
let b = 20;        


Output:

ReferenceError: Cannot access 'b' before initialization        


Explanation:

  • Variables declared with let are in the TDZ from the start of the block until their declaration.
  • Attempting to access b before its declaration results in a ReferenceError because b is in the TDZ.


Question No. 4


Code:

var x = 100;

function shadowExample() {
    var x = 200;
    console.log(x);
}

shadowExample();
console.log(x);        


Output:

200
100        


Explanation:

  • The inner x inside shadowExample shadows the outer x in the global scope.
  • Inside the function, x refers to the inner variable, logging 200.
  • Outside the function, the global x remains unchanged, logging 100.


Question No. 5


Code:

function pollution() {
    y = 30;
}

pollution();
console.log(y);        


Output:

30        


Explanation:

  • Not declaring y with var, let, or const makes it an implicit global variable.
  • The global variable y is created and accessible outside the function, logging 30.


Question 6:


Code:

console.log(typeof myFunc);

var myFunc = function() {
    return "Hello, World!";
};        


Output:

undefined        


Explanation:

  • The variable myFunc is hoisted, but its assignment as a function is not.
  • Before the assignment, myFunc is undefined, so typeof myFunc returns undefined.


These interview questions provide a solid foundation for understanding hoisting, scoping, variable shadowing, and other JavaScript quirks. Mastering these concepts will help you navigate common pitfalls and write more predictable, maintainable code.

要查看或添加评论,请登录

Uvesh Chamadiya的更多文章

社区洞察

其他会员也浏览了