Skip to content
Algorithms for Beginners

Learn / Lesson 3 of 10

Arrays

Keep many values in order, and reach any one of them by its position.

Words on this page, in plain English (11)Open if any word is new to you
Variable
A labelled box that holds a value. You give it a name, put something in it, and use the name later to get it back.
Boolean (true or false)
A value that can only be true or false. Like a light switch: on or off.
Array (list)
A list of values kept in order, inside square brackets. Like a row of numbered lockers.
Index (position)
Where an item sits in a list. Counting starts at 0, not 1, so the first item is at position 0.
Length
How many items a list (or letters a string) has. The last position is always length - 1.
Loop
Code that repeats. A for loop repeats once per item or a set number of times; a while loop repeats as long as something stays true.
console.log
Prints something so you can see it. The easiest way to find out what your code is really doing.
Pattern
A trick that solves a whole family of problems. Learn the trick once, and you can solve problems you've never seen.
Sorted
In order, smallest to biggest (or A to Z). Many fast tricks only work on sorted lists.
Stack
A pile where you only touch the top: add with push, take with pop. The last thing in is the first out, like a pile of plates.
Bug, debugging
A bug is a mistake in code. Debugging is finding it, often by printing values with console.log.

Every word, explained simply

Many values, in order

An array holds a list of values, in order. Write it with square brackets, with commas between the items:

Try it: an array

const scores = [12, 7, 30, 4];
console.log(scores);
console.log(scores.length);   // how many items

Press Run to see the output. You can change the code first.

Positions start at 0

Each item has a position, also called an index. The first item is at position 0, not 1. So in a list of 4 items, the positions are 0, 1, 2 and 3, and the last one is at length - 1.

Try it: reading by position

const scores = [12, 7, 30, 4];
console.log(scores[0]);                  // the first item
console.log(scores[2]);                  // the third item
console.log(scores[scores.length - 1]);  // the last item
console.log(scores[10]);                 // no item there: undefined

Press Run to see the output. You can change the code first.

Starting at 0 feels odd at first. Think of the index as "how many steps from the start": the first item is 0 steps in.

Reading a position that doesn't exist gives undefined, not an error. That's a very common source of bugs: if something is unexpectedly undefined, check your positions.

Changing an array

You can replace an item by its position, and add or remove items at the end:

Try it: changing items

const colours = ["red", "green"];
colours[1] = "blue";       // replace position 1
colours.push("yellow");    // add to the end
console.log(colours);

const last = colours.pop(); // remove from the end, and get it back
console.log(last, colours);

Press Run to see the output. You can change the code first.

Wait: colours is a const, but we changed it? const means the variable always points to the same array. The array's contents can still change. What you can't do is make colours point to a different array.

Going through every item

Most of the time you'll want to do something with each item. A for...of loop visits them in order (loops get their own lesson soon):

Try it: every item

const prices = [4, 10, 6];
let total = 0;
for (const price of prices) {
total = total + price;
}
console.log("Total:", total);

Press Run to see the output. You can change the code first.

Useful array tools

CodeWhat it does
list.lengthhow many items
list.push(x)add x to the end
list.pop()remove and return the last item
list.includes(x)true if x is in the list
list.indexOf(x)the position of x, or -1
list.slice(a, b)a new array with positions a up to (not including) b
[...list]a copy of the list
list.sort((a, b) => a - b)sort numbers from smallest to largest

Try it: a few tools

const nums = [5, 3, 9, 1];
console.log(nums.includes(9));
console.log(nums.indexOf(3));
console.log(nums.slice(1, 3));

const sorted = [...nums].sort((a, b) => a - b);
console.log(sorted, nums); // the copy is sorted; the original isn't

Press Run to see the output. You can change the code first.

Heads up: includes and indexOf look through the list item by item. For a long list asked many times, that gets slow. The patterns section shows faster ways.

Your turn

Make an array of five temperatures. Show the first one, the last one, and how many there are. Then add a sixth with push and show the array.

Your turn

const temps = [];

Press Run to see the output. You can change the code first.

What you learned

  • An array is an ordered list: [a, b, c].
  • Positions start at 0; the last is length - 1.
  • A missing position gives undefined.
  • push and pop add and remove at the end.
  • for...of visits every item.
Next: Strings →