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 "days = ["Monday", "Tuesday"];" as "days gets the array containing the strings "Monday" and "Tuesday". 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.
// Flip a coin var coinFaces = ["heads","tails"]; var myFlip = randomNumber(1); console.log("You flipped " + coinFaces[myFlip]);
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"); }
Example: Connect the Dots Plot a path along verticies in an array. Each vertex is also an array of two values.
// Plot a path along verticies in an array. Each vertex is also an array of two values. var coordinates = [[10,10], [100,10], [100,100], [10,100], [10,10]]; penUp(); moveTo(coordinates[0][0],coordinates[0][1]); penDown(); penWidth(3); penColor("red") for(var i=0;i<coordinates.length;i++) { moveTo(coordinates[i][0], coordinates[i][1]); }
var list = ["a","b","d"];
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 _. |
["a","b","d"] | array of strings | 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 = ["a","b","d"];
Found a bug in the documentation? Let us know at documentation@code.org