CodeMasteryLab
Section 2

Variables, Data Types & Type System

Deep dive into var, let, const, all JS data types, type coercion, and the typeof quirks that trip up developers in interviews.

Variables, Data Types & Type System

This topic is asked in virtually every JS interview — from fresher to senior level.

var vs let vs const

Feature var let const
Scope Function Block Block
Hoisted Yes (undefined) Yes (TDZ) Yes (TDZ)
Re-declare Yes No No
Re-assign Yes Yes No
var x = 1;
if (true) {
  var x = 2; // same variable!
  let y = 3; // block scoped
}
console.log(x); // 2
console.log(y); // ReferenceError

Primitive vs Reference Types

Primitives (stored by value): String, Number, BigInt, Boolean, undefined, null, Symbol

Reference types (stored by reference): Object, Array, Function

let a = 5;
let b = a; // copy of value
b = 10;
console.log(a); // 5 — unaffected

let obj1 = { name: 'Alice' };
let obj2 = obj1; // same reference
obj2.name = 'Bob';
console.log(obj1.name); // 'Bob' — affected!

Type Coercion & == vs ===

2 == '2'   // true  (coercion happens)
2 === '2'  // false (no coercion)
null == undefined  // true
null === undefined // false

Always use === in production code.