Bird
0
0

What will be the output of this Node.js code snippet?

medium📝 component behavior Q13 of 15
Node.js - Debugging and Profiling
What will be the output of this Node.js code snippet?
const x = 5;
console.log(x + y);
const y = 3;
A8
B5undefined
CReferenceError: Cannot access 'y' before initialization
DNaN
Step-by-Step Solution
Solution:
  1. Step 1: Understand variable hoisting with const

    Variables declared with const are not hoisted like var. Accessing before declaration causes ReferenceError.
  2. Step 2: Analyze the code execution order

    console.log(x + y); runs before y is declared, causing ReferenceError.
  3. Final Answer:

    ReferenceError: Cannot access 'y' before initialization -> Option C
  4. Quick Check:

    Access const before declaration = ReferenceError [OK]
Quick Trick: const variables cannot be used before declaration [OK]
Common Mistakes:
  • Assuming y is undefined and prints NaN
  • Thinking variables are hoisted like var
  • Expecting output 8 without error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes