Creating arrays
An array is an ordered list of values. In UrbanCart, every shopping cart is an array. Every product catalog is an array. Every list of order statuses is an array. Arrays are everywhere in JavaScript.
When a customer browses UrbanCart and adds items to their cart, those items are stored in an array. The array keeps them in order, lets you add or remove items, and lets you check what's inside.
You create an array using square brackets []. This is called an array literal:
const for arrays. The const keyword prevents reassignment of the variable, but you can still modify the array's contents (add, remove, change items). This is the standard in modern JavaScript.
Accessing elements
Every item in an array has an index — its position number. Indexes start at 0, not 1. This is called zero-based indexing.
at(), getting the last element required cart[cart.length - 1], which is verbose. The at() method was added in ES2022 and supports negative indexes — at(-1) gives you the last item, at(-2) the second to last, and so on.
push, pop, shift, unshift
These four methods let you add and remove items from the beginning or end of an array. Think of them as the core cart operations in UrbanCart.
Adding items
push() — adds to the endunshift() — adds to the beginning
Removing items
pop() — removes from the endshift() — removes from the beginning
push() and unshift() return the new length of the array. pop() and shift() return the removed item. This is useful when you need to process the removed item — for example, moving it to a "recently removed" list.
splice and slice
While push/pop work at the ends, splice() and slice() give you precision control over any position in the array.
splice() — mutates the array
splice(start, deleteCount, ...items) removes elements and/or inserts new ones in place. It changes the original array.
slice() — does NOT mutate
slice(start, end) returns a new array containing elements from start up to (but not including) end. The original array stays unchanged.
splice mutates (changes) the original array. slice returns a new array and leaves the original alone. When in doubt, prefer slice — immutable operations are easier to debug.
indexOf and includes
Before modifying a cart, you often need to check if an item exists. These two methods handle that.
includes() when you just need a yes/no answer. Use indexOf() when you need the position of the item — for example, to remove it with splice().
Spread operator
The spread operator ... expands an array into individual elements. It's the cleanest way to copy, combine, or extend arrays without mutating the originals.
When a customer clicks "Move all to cart" on their wishlist, UrbanCart uses the spread operator to combine the wishlist array with the cart array: const updatedCart = [...cart, ...wishlist]. The original wishlist stays intact until the customer confirms.
Destructuring
Array destructuring lets you unpack values from an array into individual variables in a single line. It's clean, readable, and used everywhere in modern JavaScript and React.
...rest: The three dots before a variable name in destructuring collects all remaining elements into a new array. It must be the last item in the destructuring pattern. You'll see this everywhere in React components and API responses.
Try It Yourself #1 — Shopping cart
Manage an UrbanCart shopping cart: add items, remove items, and check if an item exists. Edit the code and click Run to see the output.
Try It Yourself #2 — Combine and destructure
Combine two product arrays using the spread operator and then destructure the result. Edit the code and click Run to see the output.
Test your knowledge
cart.includes("Mouse") return if "Mouse" is in the cart?const [first, ...rest] = ["A", "B", "C", "D"] assign to rest?You just mastered JavaScript arrays.
Now build a full-stack career.
Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses →