Understanding Hoisting in JavaScript: var, let, const

🔥 JavaScript Interview Series(2): Hoisting, Scope & Closures 1. What's the output of console.log(x); var x = 5; and why? Core Concept: Hoisting with var. Standard Answer: The output will be undefined. This is because of hoisting, a JavaScript mechanism where variable declarations using var are moved to the top of their scope before code execution. So, the code is interpreted as if it were written like this: var x; console.log(x); x = 5; When console.log(x) is called, x has been declared but not yet assigned a value, so its value is undefined. Possible Follow-up Questions: Want to test your skills? Try a Mock Interviews) How would the result differ if let or const were used instead of var? Can you explain the difference between a function declaration and a function expression in the context of hoisting? How does hoisting affect variables within a function scope versus the global scope? Core Concept: Hoisting with let and const. Standard Answer: The Temporal Dead Zone is the period between entering a scope and where a let or const variable is d https://lnkd.in/gbaUz_ym

To view or add a comment, sign in

Explore content categories