JavaScript Data Types & Type Coercion
1. Simple Definition
JavaScript me store hone wali har value ki ek specific Data Type hoti hai. JavaScript ek dynamically typed language hai, jiska matlab hai ki hume variable ka type manually declare nahi karna padta (jaise C ya Java me int x likhte the). JavaScript values do categories me aati hain: 7 Primitives aur Reference (Objects).
2. Real-Life Analogy
• String: Text message ("Namaste Amit").
• Number: Jeb me rakhe rupaye (₹499 ya 3.14).
• Boolean: Bijli ka switch (Sirf On ya Off -> true / false).
• undefined: Khali packet jisme abhi tak kuch daala hi nahi gaya.
• null: Janbujhkar khali chhod diya gaya dabba ("Intentional emptiness").
• Object: Ek student ka complete bag jisme tiffin, kitabein aur pen sab ek sath grouped hain!
3. Why Do We Need It?
Computer ko pata hona chahiye ki do values ke sath kya action karna hai. Agar aap 2 + 2 karte hain toh 4 hona chahiye, lekin agar "2" + "2" karte hain toh "22" text judna chahiye.
4. The 7 Primitive Types & Reference Type
// 1. String
const name = "Pooja";
// 2. Number (Integers & Decimals both are Number!)
const age = 22;
const rating = 4.8;
// 3. Boolean
const isLoggedIn = true;
// 4. Undefined (Declared but no value assigned yet)
let userLocation; // Value is undefined
// 5. Null (Explicitly empty)
const selectedProduct = null;
// 6. BigInt (Large numbers beyond 2^53 - 1)
const hugeNumber = 9007199254740991n;
// 7. Symbol (Unique identifier)
const uniqueKey = Symbol("id");
// 8. Reference Type: Object (Arrays & Functions are also Objects!)
const userProfile = { name: "Amit", age: 24 };
const skills = ["HTML", "CSS", "JS"];
5. Basic Example: typeof Operator
console.log(typeof "Hello"); // "string"
console.log(typeof 100); // "number"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" ⚠️ (JS historic bug!)
console.log(typeof [1, 2, 3]); // "object"
console.log(typeof function(){});// "function"
6. Output / Expected Result
7. Code Explanation: Primitive vs Reference in Memory
- Primitives (Immutable): Stack memory me direct value store hoti hai. Value copy karne par ek independent nayi copy banti hai.
- Reference Types (Mutable): Heap memory me data store hota hai aur variable ke paas sirf uska address (reference) hota hai. Ek jagah badalne par doosri jagah bhi badal jata hai!
8. Real-World Example
Authentication me: Jab user ne profile photo upload nahi ki hoti, toh database se avatar = null; aata hai, jisse UI me default fallback avatar render hota hai.
9. Common Mistakes
undefined ka matlab hai: "System ne abhi tak value assign nahi ki".
null ka matlab hai: "Developer ne janbujhkar khali chhod diya hai".
null == undefined true hota hai, lekin null === undefined false hota hai!
Agar aap const user2 = user1; user2.name = "John"; karte hain, toh user1.name bhi badal jata hai! Real copy ke liye { ...user1 } spread use karein.
10. Best Practices
- Array check karne ke liye
typeof arr(jo "object" deta hai) ke badleArray.isArray(arr)use karein. - Missing value set karne ke liye
undefinedmanually likhne ke badlenulluse karein.
11. Try It Yourself
Playground me console.log(typeof NaN); likhein aur dekhein ki NaN ka type kya aata hai!
12. Challenge
Ek variable banayein jisme user ka roll number string me ho ("101") aur use Number conversion se actual number banayein (Number("101")).
13. Interview Questions
Answer: Ye 1995 me JavaScript ke pehle release ka ek historic implementation bug hai. JS engine me type tags 32 bits ke hote the aur objects ka tag 000 tha. Null pointer bhi 0x00 tha, isliye typeof null "object" return kar gaya. Is bug ko fix nahi kiya gaya kyunki isse dunya ki lakho puraani websites toot jaati!
14. Quick Revision
- 7 Primitives: string, number, boolean, null, undefined, bigint, symbol.
- Primitives copy by value; Objects copy by reference.
Array.isArray(val)checks arrays accurately.
15. FAQ
Q1. NaN kya hai?
NaN ka matlab hai "Not a Number". Ye tab aata hai jab invalid math operation ho (e.g. "hello" / 2). Ajeeb baat ye hai ki typeof NaN "number" hi hota hai!
Q2. BigInt ki zarurat kyun padi?
Standard JS Number 9,007,199,254,740,991 se bade numbers me precision lose kar deta tha (crypto aur bank transactions ke liye BigInt zaroori hai).
Q3. Explicit conversion kaise hoti hai?
String(123), Number("456"), Boolean(1) functions use karke.
Q4. Falsy values kaunsi hain JS me?
Sirf 8 falsy values hain: false, 0, -0, 0n, "", null, undefined, NaN. Baaki dunya ki har cheez truthy hoti hai!
Q5. Symbol ka use case kya hai?
Objects ke andar guaranteed unique private keys banane ke liye Symbol use hota hai.
JavaScript Data Types & Type Coercion
1. Simple Definition
JavaScript is a dynamically typed programming language where variables can hold values of various Data Types without explicit type declarations. JavaScript values are categorized into 7 Primitive Types (stored directly by value) and Reference Types (stored in heap memory by reference).
2. Real-Life Analogy
• Primitive (e.g. Number, String): Giving someone a 10-dollar bill. They now have their own independent bill; spending it doesn't diminish the cash in your pocket.
• Reference (Object, Array): Handing a friend a duplicate key to your shared bank locker. If they open the locker and withdraw an item, the change is reflected when you open the locker too!
3. The 7 Primitive Data Types
| Data Type | Example | Description |
|---|---|---|
number |
42, 3.14, NaN, Infinity |
64-bit floating-point numbers |
string |
"Code", 'Web', `Hello` |
Sequence of textual characters |
boolean |
true, false |
Binary logical truth values |
undefined |
let x; (Value not assigned) |
Default state of uninitialized variables |
null |
let car = null; |
Intentional absence of any object value |
symbol |
Symbol("id") |
Guaranteed unique identifier |
bigint |
9007199254740991n |
Arbitrary-precision integers beyond 2^53 - 1 |
4. The typeof Operator (And Its Famous Bug)
typeof 42; // "number"
typeof "Alex"; // "string"
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof Symbol(); // "symbol"
// ⚠️ Famous 1995 JavaScript Bug:
typeof null; // "object" (Legacy engine bug preserved for web compatibility)
5. Implicit Type Coercion
// String concatenation wins with the + operator:
console.log("5" + 2); // "52" (String)
// Mathematical operations convert strings to numbers:
console.log("5" - 2); // 3 (Number)
console.log("5" * "2"); // 10 (Number)
console.log("hello" - 2); // NaN (Not a Number)
6. Common Mistakes
In JavaScript, NaN === NaN evaluates to false! Always use the native method Number.isNaN(value) to verify whether a calculation produced NaN.
7. Best Practices
- Use strict explicit type conversion (e.g.
Number(input)orString(num)). - Never rely on implicit type coercion for critical business calculations.
- Distinguish
null(explicitly cleared by developer) fromundefined(unassigned by engine).
8. Practice Exercise
Predict the output of the following expressions and verify them in your console:
1. true + false
2. [] + {}
3. "10" - - "5"
9. Interview Questions
Answer: undefined means a variable has been declared but has not yet been assigned any value. null is an intentional assignment representing an explicit empty value or absence of an object.
10. Summary / Cheat Card
- 7 Primitives: string, number, boolean, undefined, null, symbol, bigint.
- Primitives are passed by value; Objects are passed by reference.
typeof null === "object"is a documented legacy quirk.
11. FAQ
Q1. What is NaN in JavaScript?
NaN stands for "Not-a-Number", but its data type is actually "number". It represents the failed result of an invalid mathematical operation (e.g. dividing text by numbers).
Q2. How do you check if a value is an Array?
Use Array.isArray(variable) since typeof [] returns "object".