KandZ – Tuts

We like to help…!

JavaScript 7 🧬 How to variable hoisting and var vs let

1. How to access var variables before declaration

Steps:

  1. Declare a variable using var
  2. Use the variable before its declaration
  3. 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:

  1. Declare a function after using it
  2. Call the function before its declaration
  3. 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:

  1. Declare variable with var
  2. Log variable before assignment
  3. 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:

  1. Declare variable with let
  2. Try to access before declaration
  3. 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:

  1. Declare same variable name with var twice
  2. No error occurs
  3. 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:

  1. Try to declare same variable with let
  2. 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:

  1. Declare variable inside function using var
  2. Try to access outside function
  3. 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:

  1. Declare variable with let inside block
  2. Try to access outside block
  3. 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:

  1. Declare var variable
  2. Reassign value multiple times
  3. 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:

  1. Declare let variable
  2. Reassign value multiple times
  3. 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:

  1. Declare let variable after access attempt
  2. Observe ReferenceError
  3. 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:

  1. Use var inside function before declaration
  2. Variable becomes undefined when accessed
  3. 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:

  1. Declare loop variable with let
  2. Use it multiple times in same scope
  3. 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:

  1. Compare same variable with both declarations
  2. Observe different access patterns
  3. 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:

  1. Use let or const instead of var
  2. Avoid re-declaring same variable names
  3. 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:

  1. Declare functions at bottom of scope
  2. Call them before declaration
  3. 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:

  1. Always declare let variables before use
  2. Place declarations at start of scope
  3. 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:

  1. Create nested functions with var
  2. Access variables from inner scope
  3. 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:

  1. Declare let at global level
  2. Declare same name inside block
  3. 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:

  1. Check if accessing before declaration works
  2. Observe undefined vs ReferenceError
  3. 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!

Leave a Reply