JavaScript Variable Scope Interview Trap

🔥 JavaScript can “teleport” your variables. This is a classic interview trap — and it still catches experienced devs 👇 ❓What prints for console.log(a)? A) 10 B) undefined C) ReferenceError D) null ❓What happens at console.log(b)? A) 20 B) undefined C) ReferenceError D) null 💬 Drop your answers + reasoning below 👇 #CodeSnatch #javascript #webdevelopment #frontend #interviewprep #codinginterviews #developers

  • No alternative text description for this image

undefined ReferenceError Both 'var' and 'let' are hoisted to the top, BUT... 👉 var a is hoisted (declared at the top of its scope), but its assignment (= 10) is not. 👉 let is hoisted too, but it’s in the Temporal Dead Zone (TDZ) until the declaration line runs.

To view or add a comment, sign in

Explore content categories