devnotes.

Working with Arrays

Arrays are ordered, zero-indexed collections built on top of JavaScript's object system.

const nums = [1, 2, 3];
nums.push(4);      // adds to end   -> [1, 2, 3, 4]
nums.pop();         // removes from end -> returns 4
nums.unshift(0);   // adds to front -> [0, 1, 2, 3]
nums.shift();       // removes from front -> returns 0

console.log(nums.includes(2)); // true
console.log(nums.length);      // 3

push/pop operate at the end of the array and are fast — O(1) — because nothing else needs to move. unshift/shift operate at the start and are slower — O(n) — because every remaining element has to be re-indexed.

Arrays also support direct indexing (nums[0]) and the modern .at() method (ES2022), which accepts negative indices: nums.at(-1) returns the last element without needing nums.length - 1.