Category: Variables
Declares a variable and assigns it to an array with the given initial values.
Many apps process collections of data. To be able to process collections of data your apps need to keep track of the data in memory. Array variables are simply names you use to refer to stored data in your apps. You can name your variables whatever you want so long as the name is not already used by the system. Variable names can not have spaces or special characters. In practice, it is helpful to name your variables in a way that describes the value they store. For instance, if the array variable you create is to store a days of the week you might name that array variable daysOfWeek.
In addition to the array variable name, items in your array are numbered with an index. The first element in an array is has index 0 and the second had index 1 and so on. As a result the last index is always one less than the length of the array.
To process collections of data in our apps we need to assign values to memory locations we have previously named using var to declare an array variable. Programmers read the statement "numbers = [10, 20];" as "numbers gets the array containing the integers 10 and 20." With this type of assignment, the variable is assigned to an array of values. The brackets denote the start and end of the array and the commas delimit the array values.
Name | Type | Required? | Description |
---|---|---|---|
list | variable name | Yes | The name you will use in the program to reference the variable. Must begin with a letter, contain no spaces, and may contain letters, digits, - and _. |
[1,2,3] | array of integers | Yes | The initial values to the array enclosed in square brackets and separated by commas. |
No return value. Array variable created in memory and values assigned.
Found a bug in the documentation? Let us know at documentation@code.org
var list = [1,2,3];
Found a bug in the documentation? Let us know at documentation@code.org