Arrays
What's an Array?
An array is like a row of lockers — internally it works like a dynamic array. Each locker has a number (starting from 0), and each one holds a value. You can open any locker by its number, add new lockers at the end, remove one from the front, or rearrange them however you like.
Arrays are one of the most-used data structures in JavaScript. They're ordered (position matters) and can hold any mix of types — numbers, strings, objects, even other arrays.
Creating & Accessing Arrays
Adding & Removing Items
Arrays have built-in methods to add and remove items from either end. Think of it like a line at a theme park:
.push(item)— add to the end of the line.pop()— remove from the end.unshift(item)— cut to the front of the line.shift()— remove from the front.splice(index, count, ...newItems)— the Swiss Army knife: insert, remove, or replace items anywhere
push and pop are fast (they just touch the end — like a stack). unshift and shift are slower because every other item has to scoot over.
Push, Pop, Shift, Unshift & Splice
Spread Operator & Destructuring
The spread operator (...) is like dumping a box of LEGO bricks onto the table — it takes all the items out of an array and spreads them individually. It's incredibly useful for copying arrays, merging them, or passing items as arguments.
Destructuring lets you unpack values from an array into named variables in a single line. Instead of writing arr[0], arr[1], you can pull values out by position.
Spread & Destructuring
Quick check
Continue reading