Arrays

An array is a group of values that are stored in a particular order. Usually these values are all of the same type (like all numbers, all text, or even all sprites), but in JavaScript, arrays can also hold different types of elements. You can use arrays to create a list of things that you store into a single variable.

Creating and Using Arrays

Arrays look like lists inside square brackets. Each value in the array should be separated out with commas.

[1,5,7,2]

Every time you create an array, you’ll need to store it into a variable right away so that you can access the values later. Because this is so important, the block to create an array automatically stores the array into a variable.

You can access a value inside your array by using its index number. The index numbers start from 0 and count up until they get to the end of the array. For example, in the above array, “a” is at index 0, “b” is at index 1, and “c” is at index 2.

Found a bug in the documentation? Let us know at documentation@code.org