Understanding Variables and Data Types in JavaScript

Start with a real-life analogy (a box storing information
π¦ What Are Variables? (Real-Life Analogy First)
Imagine you have a box at home.
You put your shoes inside one box.
Your documents inside another box.
Your mobile charger inside a different box.
To remember whatβs inside, you label each box:
ShoesBoxDocsBoxChargerBox
π In programming, a variable is just like that labeled box.
It:
Stores information
Has a name (label)
Can hold different types of values (number, text, true/false, etc.)
π» Variables in JavaScript
In JavaScript, we create variables using:
let age = 25;
const name = "Ankita";
var city = "Nagpur";
Here:
ageβ box storing number25nameβ box storing text"Ankita"cityβ box storing text"Nagpur"
πΉ 2οΈβ£ Code Execution Phase
Step-by-step:
console.log(a)β printsundefineda = 10
Final Memory:
| Variable | Value |
|---|---|
| a | 10 |
π Execution Context Diagram
π Why Are Variables Needed?
Variables are used to store data in memory so we can:
Reuse values
Update values
Perform operations
Avoid repeating code
Make programs dynamic
Example (Without Variable β)
console.log(50000 + 10000);
console.log(50000 + 10000);
Example (With Variable β )
let salary = 50000;
let bonus = 10000;
console.log(salary + bonus);
π Variables make code readable, reusable, and maintainable.
| Feature | var |
let |
const |
|---|---|---|---|
| Scope | Function | Block | Block |
| Re-declare | β Yes | β No | β No |
| Update | β Yes | β Yes | β No |
| Hoisting | β Yes (undefined) | β Yes (TDZ) | β Yes (TDZ) |
| Global Object | β Yes | β No | β No |
| Example | var a=10; var a=20; a=30; |
let b=10; b=20; |
const c=10; |
| Block Test | if(true){var x=1} console.log(x) β
|
if(true){let y=1} console.log(y) β |
if(true){const z=1} console.log(z) β |
| Pros | Old browser support | Safe block scope | Safest, prevents reassignment |
| Cons | Re-declare bugs, global leak | TDZ confusion | Must initialize, no reassignment |
π Best Practice Rule
π Use const by default
π Use let when value needs to change
π Avoid var in modern JavaScript
π₯ Temporal Dead Zone (TDZ) β Detailed Explanation
π What is TDZ?
TDZ is the time between:
π Variable hoisting
π And actual initialization
During this period, accessing variable gives error.
πΉ Example
console.log(a); // undefined
var a = 10;
console.log(b); // β ReferenceError
let b = 20;
Why?
varis initialized withundefinedlet&constare hoisted but NOT initialized
π₯ TDZ Diagram
Memory Phase:
b β uninitialized
Execution Phase:
Line 1: console.log(b)
β Error (Still in TDZ)
Line 2: let b = 20;
b initialized
πΉ Another Example
{
console.log(x); // β TDZ
let x = 5;
}
TDZ starts at beginning of block {} and ends at declaration line.
π‘ Final Summary
Variables store data.
JS creates execution context in 2 phases.
varis function scoped & hoisted with undefined.let&constare block scoped & have TDZ.TDZ prevents early access errors.
Prefer
const>let> avoidvar.
β Primitive Data Types in JavaScript
JavaScript has 7 primitive data types, but here weβll focus on the main 5 you mentioned:
| Data Type | Description | Example | Type Of |
|---|---|---|---|
| string | Text data | "Hello" |
"string" |
| number | Numbers (integer & decimal) | 10, 3.14 |
"number" |
| boolean | True or False | true, false |
"boolean" |
| null | Intentional empty value | let x = null |
"object" β οΈ (JS bug) |
| undefined | Variable declared but not assigned | let x; |
"undefined" |
π§ Example Code
let name = "Ankita"; // string
let age = 25; // number
let isStudent = true; // boolean
let city = null; // null
let marks; // undefined
console.log(typeof name); // string
β‘ Super Important Interview Example
if (true) {
var a = 10;
let b = 20;
}
console.log(a); // β
10
console.log(b); // β Error
π var ignores block scope
π let & const respect block scope
| Variable | Inside Block | Outside Block | Outside Function |
|---|---|---|---|
| var | β Yes | β Yes | β No |
| let | β Yes | β No | β No |
| const | β Yes | β No | β No |
π― Quick Summary
Primitive types store single simple values.
var= function scoped (old way)let= block scoped & changeableconst= block scoped & not changeablePrefer let & const in modern JavaScript.




