JavaScript లో 7 primitive types ఉన్నాయి, మరియు మిగతా ప్రతిదీ (arrays, functions, objects) object.
Primitives: string, number, boolean, null, undefined, bigint, symbol.
js
typeof "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 మార్పులేనివి మరియు విలువ ద్వారా నకల్ చేయబడతాయి; objects సూచన ద్వారా నకల్ చేయబడతాయి.
js
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" సుదీర్ఘ కాలం నుండి ఉన్న భాష బగ్ ఇది, మీరు గుర్తుంచుకోవాలి — null కోసం చెక్ చేయడానికి, నేరుగా పోల్చండి (x === null).
ఇది ఎందుకు ముఖ్యమైనది
Value-vs-reference ని అర్థం చేసుకోవడం ఇలా వివరిస్తుంది: ఎందుకు పంచుకున్న object ను మార్చడం రెండు variables ను ప్రభావితం చేస్తుంది, {} === {} ఎందుకు false, మరియు React వంటి state-driven UI లలో objects ను మార్చడానికి ముందు ఎందుకు నకల్ చేస్తారు (spread).
