JavaScript 7 🧬 How to variable hoisting and var vs let
1. How to access var variables before declaration
Steps:
- Declare a variable using
var - Use the variable before its declaration
- Log the variable value
console.log(x); // undefined (not error)
var x = 5;
Why: var variables are hoisted with default value undefined
2. How to access function declarations before definition
Steps:
- Declare a function after using it
- Call the function before its declaration
- Function works normally
greet(); // "Hello!"
function greet() {
console.log("Hello!");
}
Why: Function declarations are fully hoisted to top of scope
3. How to see undefined value from var hoisting
Steps:
- Declare variable with
var - Log variable before assignment
- Observe undefined output
console.log(x); // undefined
var x = 5;
Why: Hoisted var variables are initialized with undefined
4. How to use let in temporal dead zone
Steps:
- Declare variable with
let - Try to access before declaration
- Get ReferenceError
console.log(a); // ReferenceError
let a = 10;
Why: let variables are in temporal dead zone until declared
5. How to declare var multiple times
Steps:
- Declare same variable name with
vartwice - No error occurs
- Last declaration takes effect
var d = 20;
var d = 30; // No error
console.log(d); // 30
Why: var allows re-declaration in same scope
6. How to avoid let re-declaration errors
Steps:
- Try to declare same variable with
let - Get SyntaxError when trying to redeclare
let e = 40;
let e = 50; // SyntaxError
Why: let doesn’t allow re-declaration in same scope
7. How to understand function scope with var
Steps:
- Declare variable inside function using
var - Try to access outside function
- Get ReferenceError
function test() {
var localVar = "test";
}
test();
console.log(localVar); // ReferenceError
Why: var is function-scoped, not block-scoped
8. How to demonstrate block scope with let
Steps:
- Declare variable with
letinside block - Try to access outside block
- Get ReferenceError
if (true) {
let blockVar = "block";
}
console.log(blockVar); // ReferenceError
Why: let is block-scoped, only available within block
9. How to see var re-assignment works
Steps:
- Declare
varvariable - Reassign value multiple times
- No errors occur
var x = 5;
x = 10;
x = 15; // No error
console.log(x); // 15
Why: var allows re-assignment and re-declaration
10. How to demonstrate let re-assignment works
Steps:
- Declare
letvariable - Reassign value multiple times
- No errors occur
let y = 5;
y = 10;
y = 15; // No error
console.log(y); // 15
Why: let allows re-assignment but not re-declaration
11. How to understand temporal dead zone with let
Steps:
- Declare
letvariable after access attempt - Observe ReferenceError
- Then declare and use normally
console.log(b); // ReferenceError
let b = 20;
Why: let variables are inaccessible until their declaration
12. How to handle var hoisting in functions
Steps:
- Use
varinside function before declaration - Variable becomes undefined when accessed
- No runtime error but undefined value
function example() {
console.log(x); // undefined
var x = 5;
}
example();
Why: var hoisting makes variable accessible with undefined value
13. How to use let in loops without re-declaration issues
Steps:
- Declare loop variable with
let - Use it multiple times in same scope
- No re-declaration errors
for (let i = 0; i < 3; i++) {
console.log(i);
}
// No error even though let is used in loop
Why: let creates new binding for each iteration
14. How to identify var vs let behavior differences
Steps:
- Compare same variable with both declarations
- Observe different access patterns
- Notice hoisting vs temporal dead zone
// var behavior
console.log(varVar); // undefined
var varVar = "var";
// let behavior
console.log(letVar); // ReferenceError
let letVar = "let";
Why: var and let have fundamentally different scoping and hoisting behaviors
15. How to prevent var re-declaration issues
Steps:
- Use
letorconstinstead ofvar - Avoid re-declaring same variable names
- Use strict mode for better error detection
'use strict';
let x = 1;
// let x = 2; // Would cause error in strict mode
Why: Modern declarations prevent accidental re-declarations
16. How to handle var function hoisting properly
Steps:
- Declare functions at bottom of scope
- Call them before declaration
- Function works normally due to hoisting
greet(); // Works fine
function greet() {
console.log("Hello");
}
Why: Function declarations are hoisted completely to top
17. How to avoid temporal dead zone issues
Steps:
- Always declare
letvariables before use - Place declarations at start of scope
- Use modern ES6+ syntax
let myVar; // Declare first
myVar = "value"; // Then assign
console.log(myVar); // Works fine
Why: Proper declaration order prevents ReferenceError
18. How to understand var’s function scoping
Steps:
- Create nested functions with
var - Access variables from inner scope
- Variables available throughout function scope
function outer() {
var outerVar = "outer";
function inner() {
console.log(outerVar); // Works - accessible in inner scope
}
return inner;
}
Why: var is function-scoped, not block-scoped
19. How to handle let in different scopes
Steps:
- Declare
letat global level - Declare same name inside block
- Both variables are separate
let shared = "global";
{
let shared = "block"; // Different variable
console.log(shared); // "block"
}
console.log(shared); // "global"
Why: let creates separate bindings in different scopes
20. How to detect hoisting behavior
Steps:
- Check if accessing before declaration works
- Observe undefined vs ReferenceError
- Compare var vs let behavior
console.log(varVar); // undefined (var)
console.log(letVar); // ReferenceError (let)
var varVar = 5;
let letVar = 10;
Why: Hoisting creates different runtime behaviors for var vs let
Stop using slow, ad-bloated tool sites! 🤮
🔎 Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)
⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free
🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!