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 ਅਪਰਿਵਰਤਨਸ਼ੀਲ ਹਨ ਅਤੇ ਮੁੱਲ ਦੁਆਰਾ ਕਾਪੀ ਹੁੰਦੇ ਹਨ; objects ਸੰਦਰਭ ਦੁਆਰਾ ਕਾਪੀ ਹੁੰਦੇ ਹਨ।
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 ਨੂੰ ਸਮਝਣਾ ਸਮਝਾਉਂਦਾ ਹੈ ਕਿ ਕਿਉਂ ਸਾਂਝੀ ਹੋਈ ਇਕਾਈ ਨੂੰ ਬਦਲਣਾ "ਦੋਵਾਂ" ਵੇਰੀਏਬਲਾਂ ਨੂੰ ਪ੍ਰਭਾਵਿਤ ਕਰਦਾ ਹੈ, ਕਿਉਂ {} === {} false ਹੈ, ਅਤੇ ਕਿਉਂ ਤੁਸੀਂ ਉਨ੍ਹਾਂ ਨੂੰ React ਵਰਗੀਆਂ state-driven UIs ਵਿੱਚ ਬਦਲਣ ਤੋਂ ਪਹਿਲਾਂ objects (spread) ਨੂੰ ਕਾਪੀ ਕਰਦੇ ਹੋ।