Conquering the JavaScript Interview: Tricky Guess the Output Questions (Part 1)

Brajraj Agrawal
5 min readApr 4, 2024

--

Introduction:

We’ve all been there — the technical interview for a software developer position. You’ve brushed up on your concepts, practiced data structures and algorithms, and feel ready to tackle any challenge. But then, the interviewer throws you a curveball: a tricky JavaScript question that seems designed to trip you up.

This article focuses on a common culprit in these tricky questions — variable scope and hoisting. Understanding these concepts can make the difference between landing the job and walking away frustrated.

In this two-part series, we’ll delve deep into the intricacies of variable scope and hoisting, unraveling their mysteries one layer at a time.

To aid our exploration, we’ll simulate an interview scenario where common yet tricky questions related to variable scope and hoisting are posed. By the end of this series, you’ll not only be able to answer these questions confidently but also gain a deeper understanding of how JavaScript handles variables and hoisting behind the scenes.

Let’s first understand the concept of variable scope and hoisting:

Variable scope:

Variable scope refers to the accessibility and visibility of variables within a particular context in JavaScript. JavaScript has two main types of scope:

  1. Global Scope: Variables declared outside of any function or block have global scope, which means they can be accessed from anywhere within the JavaScript code.
  2. Local Scope: Variables declared inside a function or block have local scope, which means they are only accessible within that function or block.

Hoisting:

Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their containing scope during the compile phase. This means that regardless of where variables and functions are declared within a scope, they are hoisted to the top, allowing them to be used before they are declared.

Now, let’s delve into some code examples to see how variable scope and hoisting work in practice.

Question 1:

What will be the output of the following code snippet?

function foo() {
console.log(a);
let a = 10;
}
foo();

Output:

Explanation: This code will throw a reference error: Cannot access 'a' before initialization. Although a is declared within the function foo, it's not initialized before the console.log statement. Since let variables are not hoisted with an initial value, attempting to access a before its declaration causes a reference error.

Question 2:

What will be the output of the following code snippet?

foo();
function foo() {
console.log('Hello, world!');
}
var foo = function() {
console.log('Goodbye, world!');
};
foo();

The output will be:

Hello, world!
Goodbye, world!

Explanation:This might seem counterintuitive at first glance. However, it’s important to understand that function declarations are hoisted above variable declarations. So, when the foo function is called before the variable assignment, it logs 'Hello, world!'. Later, when foo is reassigned to an anonymous function expression, it logs 'Goodbye, world!'.

Question 3:

What will be the output of the following code snippet?

var x = 5;

function test() {
console.log(x);
var x = 10;
}

test();

Output: 5

Explanation: Hoisting comes into play here! Even though x is declared within the test function, JavaScript hoists all variable declarations to the top of their scope (in this case, the global scope). So, when console.log(x) is called, it sees the already declared x with a value of 5. Then, the new x with a value of 10 is assigned but doesn't affect the console output.

Question 4 (Let vs. Var Showdown):

What will be the output of the following code snippet?

for (let i = 0; i < 3; i++) {
console.log(i);
}

console.log(i); // Can you access i here?

Output: 0, 1, 2 (Error: i is not defined)

Explanation: This question highlights the difference between let and var. let has block-level scope, so i is only accessible within the for loop. Trying to access it outside the loop results in an error because i is not defined in the global scope.

Question 5 (Block-Level Scope with Let (ES6+):

What will be the output of the following code snippet?

if (true) {
let message = "Hello!";
}

console.log(message); // Can you access message here?

Output: ReferenceError: message is not defined

Explanation: let has block-level scope. The message variable is declared within the if block, making it inaccessible outside that block. Trying to access it from the global scope throws a ReferenceError.

Question 6 (Shadowing Fun):

What will be the output of the following code snippet?

var x = 10;

function test() {
var x = 20;
console.log(x); // Which x is accessed here?
}

test();
console.log(x); // Which x is accessed here?

Output: 20, 10

Explanation: Variable shadowing comes into play. Inside the test function, a new x is declared with a value of 20. This shadows the global x. So, the first console.log prints 20. The second console.log outside the function accesses the original global x, printing 10.

Question 7 (Tricky const Reassignment):

const obj = { name: "Alice" };

obj.name = "Bob"; // Will this change the name property?

console.log(obj.name);

Output: Bob

Explanation: const prevents you from re-assigning the variable itself, but it doesn't prevent modifying properties of objects assigned to a const variable. So, in this case, we can change the name property of the object stored in obj.

Conclusion:

In this first part of our series, we’ve explored the fundamental concepts of variable scope and hoisting in JavaScript. We’ve covered the basics of variable scope, explained hoisting, and dissected code snippets to illustrate how these concepts manifest in real-world scenarios.

Next Part:
Unraveling Advanced JavaScript Concepts: An Interviewer’s Guide(Part 2)

As we continue our journey through the ever-evolving landscape of programming, I invite you to connect with me on LinkedIn to stay updated on the latest insights, discussions, and discoveries in the world of technology.

Feel free to follow me on LinkedIn here for more engaging content and opportunities to connect with fellow enthusiasts.

Happy coding, and may your JavaScript adventures be filled with discovery and enlightenment!

--

--

Brajraj Agrawal
Brajraj Agrawal

Written by Brajraj Agrawal

Words that Build | Full-Stack Developer (MEAN Stack). Turning ideas into web apps for 8+ years.