Quick Overview
Arrays index from 0, so the last element sits at `length - 1` and `arr[arr.length]` is always `undefined`. Nearly every off-by-one bug lives at that one boundary.
An array is an ordered list of values enclosed in square brackets [...]. Array elements can hold any data type, numbers, strings, objects, or even nested arrays.
Arrays store sequences of data (user lists, product catalogs, order items) accessible by 0-based numerical index positions.
Array positions start at index 0 (first item = arr[0]). You can find the last item using arr[arr.length - 1] or modern arr.at(-1):
const fruits = ["Apple", "Banana", "Cherry"];
console.log(fruits[0]); // "Apple" (First element)
console.log(fruits[1]); // "Banana" (Second element)
console.log(fruits.length); // 3
console.log(fruits.at(-1)); // "Cherry" (Last element)
// Modifying array contents
fruits.push("Dragonfruit"); // Add item to end
console.log(fruits.length); // 4
push(item): Add to end of array.pop(): Remove item from end of array.unshift(item): Add item to start of array.shift(): Remove item from start of array.const stack = [10, 20];
stack.push(30); // [10, 20, 30]
stack.pop(); // Returns 30 -> [10, 20]
fruits[99]) returns undefined rather than throwing an error. Always check .length or verify array bounds.length = 5 has valid indices from 0 to 4. Accessing arr[5] returns undefined!Write it yourself. The tests below run against your code.
Arrays store ordered lists. Next, let's explore Objects for key-value records!
Primitives copy by value and objects copy by reference, so `b = a` duplicates a number but shares an object. That single distinction explains most surprise mutations in a codebase.
`const` prevents reassignment, not mutation: a `const` object's properties can still be changed. `let` is scoped to its block, while `var` is function-scoped and leaks out of loops.
Only backticks interpolate: `${user}` reads the variable, while the identical text in quotes prints literally. Strings are immutable, so every method returns a new one rather than editing in place.