Declare and assign an array [1,2,3] to a variable


Declare and assign an array to a variable

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.


  • Your code will be more readable if you give your variables easy to understand variable names. On the other hand, the longer the variable name, the more you have to type.
  • Because the scope is defined in the place where the variable is created, it is important to be very careful where you create variables. Most programmers like to create their variables at the top of a block of code (top of the file, top of a function, etc) so they are less likely to get moved around in later edits.
  • Off-by-one errors are very common when referencing array elements. Always pay attention to making sure you start at zero and end one less than the length of the array.
  • It is perfectly valid to have the value of an element in an array be another array. See the "Connect the Dots" example above.
  • In addition to referring to a particular item in an array using its index (list[index]), length, appendItem(), insertItem(), and removeItem() are available array functions.

Found a bug in the documentation? Let us know at


var list = [1,2,3];

Found a bug in the documentation? Let us know at