← All lessons/Arrays· Lesson 22 of 32

Making a list

Create an array with square brackets, read items by their index, and check how many it holds with .length.

About 6 min

A list of values

A variable holds one value. An array holds a whole list of values in a single variable. You write an array with square brackets [ ], separating the items with commas:

let scores = [10, 20, 30];
An array of three numbers.

Reading an item

Each item has a position called its index. Counting starts at 0, not 1 — so the first item is scores[0], the second is scores[1], and so on. Put the index in square brackets after the array's name:

Indexing starts at 0.

How many?

scores.length tells you how many items the array has — here, 3. The last item is always at index length - 1.

Your turn

Create an array named nums holding the numbers [5, 10, 15]. Then read the second item (index 1) into a variable named second, and println it.

Hints