Skip to content
BasicsChapter 5 / 47Beginner4 min read

Data Types

JavaScript data types ki poori list, 7 primitives aur objects, typeof operator aur value vs reference ka farq. Roman Urdu mein beginner guide.

On this page

1. Data types kya hain?

JavaScript mein 8 data types hain. Saat primitive hain aur ek object.

TypeExampleKis liye
string"Lahore"Text
number42, 3.14Integers aur decimals dono
bigint9007199254740993nBohot bare integers
booleantrue, falseHaan ya nahi
undefinedundefinedValue abhi di hi nahi gayi
nullnullJaan boojh kar "koi value nahi"
symbolSymbol("id")Unique key
object{}, [], functions, DateData ka collection

2. Kyun zaroori hai?

JavaScript dynamically typed hai. Variable ka type value se tay hota hai aur badal bhi sakta hai. Agar types samajh na aayein to "5" + 1 jaise results surprise karte hain.

Types na samajhnaTypes samajhna
Text aur number mix karke ajeeb resultPehle type check, phir operation
null aur undefined ko same samajhnaDono ka alag matlab samajh kar use
Object copy samajh kar original badal denaReference ka pata, zaroorat ho to copy
Bugs runtime par aate haintypeof se pehle hi check

3. Kahan use hota hai?

Real useType
User ka naam, emailstring
Price, quantity, ratingnumber
Login hai ya nahiboolean
Database ki bari IDbigint
User profile, productobject
Products ki listArray, jo object hi hai
Optional field jo khaali hainull

4. Kaise kaam karta hai?

Primitives value ki tarah copy hote hain. Objects reference ki tarah copy hote hain, yaani dono variables ek hi object ko point karte hain.

Value vs reference
let a = 10, let b = a
b badla, a wahi rehta hai (primitive copy)
const x = object, const y = x
y badla, x bhi badal gaya (same reference)

Primitives immutable hain. String ka koi character andar se badla nahi ja sakta, naya string banta hai. Kisi value ka type jaanne ke liye typeof operator use hota hai.

5. Code example

js
console.log(typeof "hello");
console.log(typeof 42);
console.log(typeof 10n);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof Symbol("id"));
console.log(typeof { a: 1 });
console.log(typeof []);
console.log(typeof null);
console.log(typeof function () {});

Output: string, number, bigint, boolean, undefined, symbol, object, object, object, function.

Value aur reference ka farq:

js
let a = 10;
let b = a;
b = 20;
console.log(a);

const user = { name: "Hina" };
const copy = user;
copy.name = "Zara";
console.log(user.name);

const realCopy = structuredClone(user);
realCopy.name = "Nida";
console.log(user.name);

Output: 10, Zara, Zara.

Code kaise kaam kar raha hai?

CheezMatlab
typeof null"object" deta hai, yeh JavaScript ka purana bug hai
typeof []"object", array check ke liye Array.isArray() use karein
typeof function"function", lekin function bhi object hi hai
b = aPrimitive ki copy, alag value
copy = userSame object ka reference
structuredClone()Object ki deep copy banata hai

Interview tip

"null aur undefined mein farq?" undefined ka matlab value abhi set nahi hui (JavaScript khud deta hai). null developer jaan boojh kar lagata hai ke "yahan koi value nahi". typeof null ka "object" aana ek historical bug hai.

6. Common mistakes

Galati: typeof se null ya array check karna

Why? Dono ke liye typeof 'object' deta hai, is liye check ghalat pass ho jata hai.

Behtar: null ke liye value === null aur array ke liye Array.isArray(value) use karein.

Galati: Object ko = se copy samajhna

Why? Sirf reference copy hota hai, ek jagah change dono jagah dikhta hai.

Behtar: Shallow copy ke liye spread syntax, deep copy ke liye structuredClone use karein.

Galati: Bare integers ke liye number use karna

Why? number sirf 2 ki power 53 tak integers exactly rakh sakta hai, us ke baad precision kho jati hai.

Behtar: Bohot bari IDs ya counts ke liye BigInt use karein, jaise 9007199254740993n.

7. Pehle aur baad mein kya seekhein?

Final quick revision

PointDetail
Total types8 (7 primitives + object)
Primitivesstring, number, bigint, boolean, undefined, null, symbol
typeof null"object", purana bug
Primitive copyValue copy hoti hai
Object copyReference copy hota hai