Module 2: Language Fundamentals
Primitive and Reference Types
Recognize JavaScript primitive values and how objects are assigned and compared.
JavaScript primitive types are string, number, bigint, boolean, undefined, symbol, and null (although typeof null has a historical quirk). Objects include ordinary objects, arrays, and functions.
const title = "JavaScript"; // string
const rating = 4.8; // number
const isPublished = true; // boolean
let missingValue; // undefined
const nothing = null; // intentional absence
const largeId = 9007199254740993n; // bigint literal
const uniqueKey = Symbol("key");
JavaScript's ordinary number type uses IEEE 754 floating-point numbers. For integer arithmetic, the largest exactly representable integer is Number.MAX_SAFE_INTEGER. BigInt supports larger integers but cannot be mixed directly with number in arithmetic.
Primitive values versus objects#
Primitive values are copied as values. Variables referencing objects hold a reference to the object, so two variables can refer to the same object:
const original = { count: 1 };
const alias = original;
alias.count = 2;
console.log(original.count); // 2
Assignment does not automatically clone an object. A shallow copy can be made with { ...original }, but nested objects remain shared unless copied separately.
typeof#
typeof "hello" returns "string"; typeof 42 returns "number"; and typeof undefined returns "undefined". Arrays and null return "object", so use Array.isArray(value) to check for an array and explicit comparisons to check for null.
Practice#
Create two variables that reference one object, mutate it through the second variable, and explain why reading the first variable shows the changed value.