Variables, Data Types & Type Coercion
Declaring Variables
| Keyword | Scope | Reassignable | Redeclarable | Hoisted |
|---|---|---|---|---|
var | Function | Yes | Yes | Yes (undefined) |
let | Block | Yes | No | No (TDZ) |
const | Block | No | No | No (TDZ) |
var a = 10; // function-scoped, avoid using
let b = 20; // block-scoped, preferred for mutable values
const c = 30; // block-scoped, cannot be reassigned
const arr = [1, 2, 3];
arr.push(4); // ✅ allowed — const prevents reassignment, not mutation
arr = [5, 6]; // ❌ TypeError: Assignment to constant variable
Primitive Data Types (7 types)
// 1. Number
let age = 25;
let pi = 3.14;
let inf = Infinity;
let nan = NaN; // "Not a Number"
// 2. String
let name = "Reet";
let greeting = 'Hello';
let template = `Hi ${name}`;
// 3. Boolean
let isActive = true;
let isDeleted = false;
// 4. undefined
let x; // declared but not assigned
// 5. null
let y = null; // intentional absence of value
// 6. Symbol (ES6)
const id = Symbol('id');
const id2 = Symbol('id');
console.log(id === id2); // false (always unique)
// 7. BigInt (ES2020)
const bigNum = 9007199254740991n;
const anotherBig = BigInt(12345678901234567890);
Reference Types (Objects)
const person = { name: "Reet", age: 25 }; // Object
const colors = ["red", "green", "blue"]; // Array
function greet() { return "Hello"; } // Function
const today = new Date(); // Date
const pattern = /[a-z]+/gi; // RegExp
Key difference: Primitives are copied by value. Objects are copied by reference.
let a = 10;
let b = a;
b = 20;
console.log(a); // 10 (unchanged, copied by value)
let obj1 = { x: 1 };
let obj2 = obj1;
obj2.x = 99;
console.log(obj1.x); // 99 (changed! shared reference)
typeof Operator
typeof 42 // "number"
typeof "hello" // "string"
typeof true // "boolean"
typeof undefined // "undefined"
typeof null // "object" ⚠️ (historical bug!)
typeof {} // "object"
typeof [] // "object" ⚠️ (arrays are objects)
typeof function(){} // "function"
typeof Symbol() // "symbol"
typeof 42n // "bigint"
Type Coercion (Implicit)
// String + Number = String concatenation
"5" + 3 // "53"
"5" + true // "5true"
// Arithmetic forces number conversion
"5" - 3 // 2
"5" * 2 // 10
true + 1 // 2
false + 1 // 1
null + 1 // 1
undefined + 1 // NaN
// Falsy values (6 total):
// false, 0, "", null, undefined, NaN
Type Conversion (Explicit)
// To Number
Number("42") // 42
Number(true) // 1
Number(false) // 0
Number(null) // 0
Number(undefined) // NaN
parseInt("42px") // 42
parseFloat("3.14") // 3.14
+"42" // 42 (unary +)
// To String
String(42) // "42"
(42).toString() // "42"
(255).toString(16) // "ff" (hex)
// To Boolean
Boolean(0) // false
Boolean("") // false
Boolean("hello") // true
!!"" // false (double negation)
Equality: == vs ===
// == (loose equality) — type coercion happens
5 == "5" // true
null == undefined // true
0 == false // true
// === (strict equality) — no type coercion
5 === "5" // false
null === undefined // false
// ALWAYS prefer === in your code!
Nullish Coalescing (??) and Optional Chaining (?.)
// ?? returns right side only if left is null or undefined
let user = null;
let name = user ?? "Guest"; // "Guest"
let count = 0 ?? 10; // 0 (0 is not null/undefined)
// || returns right side for ANY falsy value
let count2 = 0 || 10; // 10 (because 0 is falsy)
// ?. safely accesses nested properties
const user2 = null;
console.log(user2?.profile?.avatar); // undefined (no error)
console.log(user2.profile); // TypeError!
MCQ — Variables, Data Types & Type Coercion
Q1: What is the output of typeof null?
A) "null"
B) "undefined"
C) "object" ✅
D) "primitive"
Answer: C — Historical bug in JavaScript. typeof null returns "object" even though null is a primitive.
Q2: What is the output of "5" + 3?
A) 8
B) "53" ✅
C) "8"
D) TypeError
Answer: B — The + with a string causes concatenation.
Q3: Which keyword creates a block-scoped variable that CANNOT be reassigned?
A) var
B) let
C) const ✅
D) static
Answer: C
Q4: What is the output of null == undefined?
A) false
B) true ✅
C) TypeError
D) null
Answer: B — With loose equality, null and undefined are equal to each other.
Q5: What is the output of 0 ?? "default"?
A) "default"
B) 0 ✅
C) null
D) undefined
Answer: B — ?? only uses right side when left is null or undefined. 0 is neither.
Q6: How many primitive data types does JavaScript have?
A) 5 B) 6 C) 7 ✅ D) 8
Answer: C — number, string, boolean, undefined, null, symbol, bigint.
Q7: What does NaN === NaN return?
A) true
B) false ✅
C) undefined
D) TypeError
Answer: B — NaN is the only value not equal to itself. Use Number.isNaN() to check.
Q8: What happens when you do const arr = [1,2]; arr.push(3);?
A) TypeError: cannot modify const B) arr becomes [1,2,3] ✅ C) arr becomes [1,2] D) SyntaxError
Answer: B — const prevents reassignment, not mutation of the object/array.