Skip to main content

Command Palette

Search for a command to run...

Understanding Variables and Data Types in JavaScript

Updated
β€’5 min readβ€’View as Markdown
Understanding Variables and Data Types in JavaScript
A
Web Developer

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:

  • ShoesBox

  • DocsBox

  • ChargerBox

πŸ‘‰ 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 number 25

  • name β†’ box storing text "Ankita"

  • city β†’ box storing text "Nagpur"

πŸ”Ή 2️⃣ Code Execution Phase

Step-by-step:

  1. console.log(a) β†’ prints undefined

  2. a = 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?

  • var is initialized with undefined

  • let & const are 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.

  • var is function scoped & hoisted with undefined.

  • let & const are block scoped & have TDZ.

  • TDZ prevents early access errors.

  • Prefer const > let > avoid var.

βœ… 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 & changeable

  • const = block scoped & not changeable

  • Prefer let & const in modern JavaScript.