Sub topic 1
The Eight Types: Seven Primitives and Object
Prerequisites
This chapter assumes you know basic JavaScript syntax (variables, functions, operators), can work with objects and arrays, and can run code in a browser console or Node.js.
Seven Primitives and Object
JavaScript has eight types. Seven are primitives, which are immutable single values: string, number, bigint, boolean, undefined, null, and symbol. The eighth type is object, and arrays, functions, and dates all belong to it.
Values Versus References
When you assign a primitive to a new variable, the value is copied. When you assign an object, the new variable refers to the same object. Objects are compared by identity, so two objects with the same contents are not equal.
let a = 5;
let b = a;
b = 10;
console.log(a); // 5
const obj1 = { count: 5 };
const obj2 = obj1;
obj2.count = 10;
console.log(obj1.count); // 10
console.log({ x: 1 } === { x: 1 }); // falseInspecting Types with typeof
console.log(typeof 10n); // "bigint"
console.log(typeof Symbol("id")); // "symbol"
console.log(typeof null); // "object" (a historical bug)
console.log(typeof []); // "object"
console.log(typeof function () {}); // "function"The typeof operator gets two things wrong: it reports null as "object", and it reports arrays as "object" too. Use value === null and Array.isArray(value) for those cases.
null and undefined: Two Kinds of Nothing
undefined is what the language gives you for a variable that was never assigned, a property that doesn't exist, or a function that returns nothing. null is the value programmers set on purpose to mean "no value here."
Inside the Number Type
A number is a 64-bit IEEE 754 floating-point value, and that format explains several surprises:
console.log(0.1 + 0.2 === 0.3); // false
console.log(NaN === NaN); // false
console.log(Number.isNaN("abc")); // false (no coercion)
console.log(isNaN("abc")); // true (coerces first)
console.log(Object.is(-0, 0)); // false
console.log(1 / 0); // Infinity
console.log(2 ** 53 === 2 ** 53 + 1); // true: precision lostIntegers are exact only up to Number.MAX_SAFE_INTEGER, which is 9007199254740991.
BigInt and Why It Refuses to Mix with Number
A BigInt holds an integer of any size. You write one with an n suffix, as in 10n. JavaScript throws a TypeError when you mix BigInt and Number in arithmetic, because converting silently in either direction could lose precision. To combine them, convert one side explicitly with BigInt(x) or Number(x).
How Primitives Get Methods: Wrapper Objects
Primitives have no methods of their own. When you write "hi".toUpperCase(), JavaScript wraps the string in a temporary String object, calls the method on it, and then discards the wrapper. This is called autoboxing. Avoid writing new String("hi") yourself, because the result is an object, and every object is truthy, even one that wraps an empty string.
Exercise
What do typeof null and Array.isArray([]) return, and why do you need the second check?
Show answer
typeof null returns "object" because of a historical quirk, and Array.isArray([]) returns true. You need Array.isArray because typeof also returns "object" for arrays, so typeof cannot tell an array from any other object.
Exercise
Why does 1n + 1 throw a TypeError?
Show answer
JavaScript refuses to mix BigInt and Number in arithmetic because an implicit conversion could lose precision. Convert one operand explicitly, for example 1n + BigInt(1).