Declare and assign an array [1,2,3] to a variable
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.
In order 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.
Examples
Example: Prime Choice
Pick a prime number from an array.
// Pick a prime number from an array.
var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29];
var numberEndings = ["st","nd","rd","th","th","th","th","th","th","th"];
var myPick = prompt("Which of the first 10 prime numbers do you want");
if(myPick <= primes.length)
{
console.log("The " + myPick + numberEndings[myPick-1] + " prime is " + primes[myPick-1]);
}
else
{
console.log("I don't know the " + myPick + "th Prime");
}
Syntax
var list = [1, 2, 3];
Parameters
Name | Type | Required? | Description |
---|---|---|---|
list | variable name | 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 | The initial values to the array enclosed in square brackets and separated by commas. |
Returns
Tips
- 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 support@code.org.