1. var, let, and const Declarations :
var: Introduced in ES5,varis function-scoped and can be redeclared within the same scope. Variables declared withvarare hoisted to the top of their scope but are initialized withundefined.let: Introduced in ES6,letis block-scoped and cannot be redeclared within the same scope. Variables declared withletare hoisted but are not initialized, leading to a "temporal dead zone" until the declaration is encountered.const: Also introduced in ES6,constis block-scoped and must be initialized at the time of declaration. Likelet,constdeclarations are hoisted but not initialized, resulting in a temporal dead zone. Variables declared withconstcannot be reassigned.
2. Hoisting
Hoisting refers to the JavaScript engine's behavior of moving variable and function declarations to the top of their scope before code execution.
varHoisting: Variables declared withvarare hoisted and initialized withundefined. This means you can reference them before their declaration without causing an error, though the value will beundefined.
letandconstHoisting: Variables declared withletandconstare hoisted but not initialized. Accessing them before their declaration results in a ReferenceError due to the temporal dead zone.
3. Scoping?
Scoping defines where variables are accessible in your code. JavaScript has three main types of scope:
- Global Scope
- Function Scope (Local Scope)
- Block Scope
- Global Scope
A variable declared outside any function or block is globally scoped. It can be accessed anywhere in the script.
var globalVar = "I am global";
function test() {
console.log(globalVar); // Accessible inside function
}
test();
console.log(globalVar); // Accessible anywhere
output
-----
I am global
I am global
2. Function Scope (Var) : Variables declared with var are confined to the function in which they are declared. They are not accessible outside that function.
Example:
function myFunction() {
var localVar = "I exist only inside this function";
console.log(localVar); // Works
}
myFunction();
console.log(localVar); // Error: localVar is not defined
let and const are confined to the block and only accessible within that block. Shadowing happens when a variable in an inner scope has the same name as a variable in an outer scope. The inner variable overrides (shadows) the outer variable within its scope.
Example (Shadowing with let and const):
✅ Output:
📌 Explanation: The inner let x = "Inner Scope"; only exists inside the block, and once outside, the original x is still accessible.
1. Function Scope vs. Block Scope Shadowing
Example (var vs. let shadowing):
✅ Output:
📌 Explanation:
var yinside the function shadows the globalyonly inside the function.- Outside the function,
yis still"Global Scope".
2. Illegal Shadowing (with var and let)
In some cases, shadowing can cause errors, especially when var is used inside a block where let is already declared.
Example (Illegal Shadowing):
❌ Output:
📌 Why?
- You cannot declare a
varvariable if there’s already aletvariable with the same name in the outer scope. - But the reverse (shadowing
varwithlet) is allowed.
✅ Fixed Version:
3. Function Scope vs. Block Scope Shadowing (Real Use Case)
Let’s say we want to log a user’s name, but mistakenly shadow a variable inside a loop.
Example (Bug caused by Shadowing):
✅ Output:
📌 Why?
- The loop creates a new
usernamevariable inside the block, which only exists there. - Outside the block,
usernamestill refers to "Alice".
Comments
Post a Comment