JavaScript માં 7 primitive types છે, અને બાકી બધું (arrays, functions, objects) એક object છે.
Primitives: string, number, boolean, null, , , .
JavaScript માં 7 primitive types છે, અને બાકી બધું (arrays, functions, objects) એક object છે.
Primitives: string, number, boolean, null, , , .
undefinedbigintsymboltypeof "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 સમજવું તમને સમજાવે છે કે શા માટે શેર કરેલ object ને મોડિફાઈ કરવું બંને "બંને" ચલોને અસર કરે છે, શા માટે {} === {} એ false છે, અને શા માટે તમે objects (spread) ને કૉપી કરો છો તેને બદલતા પહેલાં state-driven UIs જેમ કે React માં.