Arrays: Lists of Data

JavaScript ES2024 · ✓ verified by execution on 2026-08-12

What is an Array?

In programming, we often need to store a list of related items—like a list of user names, a sequence of high scores, or the colors in a palette. An array is a data structure that lets you hold multiple values in a single variable.

You create an array using an array literal, which is simply a pair of square brackets [] enclosing a comma-separated list of values. Arrays can hold any type of data, and they can even hold mixed data types!

javascript
const colors = ["red", "green", "blue"];
console.log(colors[0]);
Output
red

Accessing Array Elements

To get a value out of an array, you use its index. JavaScript arrays are zero-indexed. This means the counting starts at 0, not 1! The very first element of an array is at index 0, the second is at index 1, and so on.

javascript
const letters = ["a", "b", "c"];
console.log(letters[2]);
Output
c

Here is a diagram showing how indices map to elements in an array:

"a" "b" "c" "d" Index 0 Index 1 Index 2 Index 3 Total Length = 4
Indices vs Length

Array Length

Every array has a length property that tells you exactly how many items are inside it. Since indices start at 0, the highest index in an array is always length - 1.

javascript
const nums = [10, 20, 30];
console.log(nums.length);
Output
3

Common Misconceptions: Out of Bounds

What happens if you try to access an index that is completely empty? In some languages, this will immediately crash your program. Let’s see what JavaScript does.

Predict the output javascript

Read the code. What exactly will it print? Commit to an answer before you look.

const arr = [1, 2];
console.log(arr[5]);
Output
undefined

JavaScript simply returns undefined. It does not throw a runtime error. This means you must be very careful when writing loops, because reading outside the array won’t crash—it will just silently give you undefined, which might cause subtle bugs later on!

Adding and Removing Elements

Arrays in JavaScript are dynamic; they can grow and shrink. The push() method adds one or more elements to the end of the array. It also returns the new length of the array. The pop() method removes the last element from the array and returns that removed element.

javascript
const stack = ["x"];
stack.push("y");
console.log(stack[1]);
Output
y
javascript
const stack2 = ["x", "y"];
const removed = stack2.pop();
console.log(removed);
Output
y

Let’s look at exactly how push and pop mutate the array over time. Run this step-through visualizer to watch the array change.

javascript · visualize
const fruits = [];
fruits.push("apple");
fruits.push("banana");
fruits.pop();

Edge Cases

Check yourself

What happens if you try to access an array index that doesn't exist?

Reveal answer

It returns undefined — In JavaScript, accessing an out-of-bounds index returns undefined rather than throwing an error.

What is the highest valid index in an array?

Reveal answer

The array's length minus 1 — Because JavaScript arrays are zero-indexed, the first item is at index 0 and the last item is at length - 1.

Can you add elements to an array declared with 'const'?

Reveal answer

Yes, const prevents reassignment, but the array itself can still be mutated — const only prevents reassigning the variable to a entirely new value. The array itself can still be changed using methods like push().

What does the push() method return?

Reveal answer

The new length of the array — The push() method adds elements to the array and returns the array's new length.

Challenges

Challenge 1 +15 XP

Complete the function to return the last element of the given array, no matter how long it is.

javascript
  • Test 1 — expects "3\n"
Show solution (0 XP)
const arr = [1, 2, 3];
console.log(arr[arr.length - 1]);

🐞 Bug Hunt +20 XP

Bug Hunt: This code is supposed to add "apple" to the end of the array and log the array. Why does it log a number instead?

This code runs. It just does the wrong thing. Read it, find the defect, fix it — the tests below decide when you are right.

javascript
  • Test 1 — expects "banana\napple\n"
Show solution (0 XP)
const arr = ["banana"];
arr.push("apple");
console.log(arr[0]);
console.log(arr[1]);