JavaScript में 7 primitive types हैं, और बाकी सब कुछ (arrays, functions, objects) एक object है।
Primitives: string, number, boolean, , , , ।
JavaScript में 7 primitive types हैं, और बाकी सब कुछ (arrays, functions, objects) एक object है।
Primitives: string, number, boolean, , , , ।
nullundefinedbigintsymboltypeof "hi"; // "string"
typeof 42; // "number" (both ints and floats)
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof 10n; // "bigint"
typeof Symbol(); // "symbol"
typeof null; // "object" ← famous historical bug!
typeof {}; // "object"
typeof []; // "object" (arrays are objects)
typeof function(){};// "function"
1. Primitives immutable हैं और value के आधार पर copy होते हैं; objects reference के आधार पर copy होते हैं।
let a = 5; let b = a; b++; // a is still 5 (independent copies)
let x = {n:1}; let y = x; y.n = 2; // x.n is now 2 (same object!)
2. typeof null === "object" एक लंबे समय से चला आ रहा language bug है जिसे आपको याद रखना चाहिए — null को check करने के लिए, सीधे compare करें (x === null)।
Value-vs-reference को समझना समझाता है कि shared object को mutate करना दोनों "दोनों" variables को क्यों प्रभावित करता है, {} === {} क्यों false है, और React जैसे state-driven UIs में उन्हें बदलने से पहले आप objects (spread) को क्यों copy करते हैं।