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) ก่อนเปลี่ยนแปลงในส่วน UI ที่ขับเคลื่อนด้วย state เช่น React