Skip to content
FORGENO ADS GAMESUnlock all $8

JavaScript · Unit 3: Working with data · Lesson 1 of 6

Arrays

An array is an ordered list of values in square brackets: const colors = ["red", "green", "blue"];

Opens this lesson in Code Academy. Lessons open one after another, and each unit opens when you pass the checkpoint quiz before it (you can take a checkpoint early to skip ahead). If this lesson is not open yet, the button shows you its unit on your map.

The idea

An array is an ordered list of values in square brackets: const colors = ["red", "green", "blue"];

Each item has an index that starts at 0: colors[0] is "red" and colors[2] is "blue". colors.length is 3, so the last item is colors[colors.length - 1].

push adds an item to the end and pop removes the last one. includes checks whether a value is in the array.

Worked example

JavaScript

const colors = ["red", "green", "blue"];
console.log(colors[0]);
console.log(colors.length);
colors.push("gold");
console.log(colors);
console.log(colors.includes("green"));

It prints

red
3
['red', 'green', 'blue', 'gold']
true

A const array can still change its contents. const only stops the name from pointing at a different array.

Key terms

Array
An ordered list in [ ]. Indexes start at 0; length counts the items; push adds one.

Practise it in Code Academy

It has 4 exercises (predict the output, fill in the blank and write code from a prompt) and takes about 7 minutes. Every answer is checked as you go, and feedback names the exact mistake. Hints and the solution are there if you get stuck.

Every lesson is free: no ads, no lives and no paywall.