Programming

array

/ uh-RAY /

An array is an ordered list of values that you keep in a single variable. Instead of three separate variables for three names, you put all three in one array and carry them around together — like a row of numbered lockers, each holding one item, all under the same name.

You reach into an array by position, called the index. The catch that trips up every beginner: counting starts at 0, not 1. So the first item is at index 0, the second at index 1, and so on. The first time you ask for 'item 1' and get the second thing, you'll remember this forever.

Because the items sit in order, an array is the natural shape for anything that's a sequence — a to-do list, the days of the week, search results. And it pairs perfectly with a loop: you write one small piece of code and let it walk down the whole array, doing the same thing to each item in turn.

const days = ["Mon", "Tue", "Wed"];
console.log(days[0]); // "Mon"
console.log(days[2]); // "Wed"
console.log(days.length); // 3

Three values in one variable; days[0] is the first, and .length tells you how many.

Zero-based indexing means the last item sits at length minus 1 — for 3 items, that's index 2.

Also called
listvector