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.

🛒
UrbanCart Example
A customer's shopping cart is an array of product names

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:

arrays.js Array Literals
Output
["Headphones", "Keyboard", "Mouse"] [] ["Wireless Mouse", 29.99, true, 150]
Best practice: Use 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.

access.js Indexing
Output
Headphones Keyboard Mouse 3 Mouse Headphones Mouse Keyboard
Why at(-1)? Before 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 end
unshift() — adds to the beginning

➖

Removing items

pop() — removes from the end
shift() — removes from the beginning

cart-ops.js Cart Operations
Output
Start: ["Headphones", "Keyboard", "Mouse"] After push: ["Headphones", "Keyboard", "Mouse", "USB Cable"] Popped: USB Cable After pop: ["Headphones", "Keyboard", "Mouse"] After unshift: ["Laptop Stand", "Headphones", "Keyboard", "Mouse"] Shifted: Laptop Stand After shift: ["Headphones", "Keyboard", "Mouse"]
Return values: 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.

splice.js Remove & Insert
Output
Removed: ["Mouse"] After splice: ["Headphones", "Keyboard", "Monitor", "Webcam"] After insert: ["Headphones", "Smartwatch", "Keyboard", "Monitor", "Webcam"] After replace: ["Headphones", "Smartwatch", "Keyboard", "USB Hub", "Webcam"]

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.

slice.js Sub-arrays
Output
Subset: ["Phone", "Tablet"] From index 2: ["Tablet", "Watch", "Earbuds"] Last two: ["Watch", "Earbuds"] Original: ["Laptop", "Phone", "Tablet", "Watch", "Earbuds"]
splice vs. slice: The names look similar but behave very differently. 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.

search.js Finding Items
Output
true false 1 -1 Mouse is already in your cart!
When to use which: Use 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.

spread.js Spread Operator
Output
New cart: ["Headphones", "Keyboard", "Mouse", "Smartwatch"] Original: ["Headphones", "Keyboard", "Mouse"] Combined: ["Laptop", "Phone", "Case", "Charger", "Cable"] Copy: ["Headphones", "Keyboard", "Mouse", "Monitor"] Original: ["Headphones", "Keyboard", "Mouse"]
🏪
UrbanCart Example
Merging wishlists with the shopping cart

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.

destructure.js Array Destructuring
Output
Headphones Keyboard Mouse Third: Mouse Top pick: Headphones Others: ["Keyboard", "Mouse", "Monitor", "Webcam"] a: Phone b: Laptop
The rest pattern ...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 — Shopping Cart Operations
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.

Try It Yourself — Spread & Destructure
Output

Test your knowledge

Question 1 of 5
What is the index of the first element in a JavaScript array?
Question 2 of 5
Which method adds an item to the END of an array?
Question 3 of 5
What is the key difference between splice() and slice()?
Question 4 of 5
What does cart.includes("Mouse") return if "Mouse" is in the cart?
Question 5 of 5
What does const [first, ...rest] = ["A", "B", "C", "D"] assign to rest?
0
/5
Great job!
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

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 →