Lists and Indexing
Two words for one thing
A run of comma-separated items inside square brackets is called an array in JavaScript. The AP CSP reference sheet calls the same thing a list, and so does most exam wording. They are two words for one thing, and this course uses both.An array keeps its items in order, and it can hold numbers, strings, booleans, or a mixture.
Printing a whole array
Handing a whole array to console.log does not print exactly what you typed. JavaScript keeps the square brackets, puts a comma and a space between items, and puts one extra space just inside each bracket.So the array written [7, 22, 35, 40] in your code prints as [ 7, 22, 35, 40 ]. Those two extra spaces are easy to miss on a trace question, so read printed arrays one character at a time.
length is a property, not a call
Every array already knows how many items it holds, and you read that count with.length, written with a dot and no parentheses. It is a property, a value the array carries, not a method you call.Adding parentheses asks JavaScript to call that number as if it were a function, and the program stops with a TypeError.
Indexing starts at 0
Fetching one item out of an array is called indexing. Write the array name, then the position inside square brackets. Positions are numbered from 0, so the first item sits at index 0 and the fourth sits at index 3.| index | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| value | 7 | 22 | 35 | 40 |
Three quiet ways to miss
This is where JavaScript differs most from other languages, and the difference is that nothing goes wrong out loud.•
temps[4] is one slot past the end. JavaScript hands back undefined, with no error and no warning.•
temps[-1] does not count backwards from the end. No slot was ever stored under the name -1, so this is undefined as well.•
temps.at(-1) is the method that really does count backwards, and it gives 40.All three run to completion. A wrong index turns up later as a strange value, or as NaN in a calculation, never as a message pointing at the line that caused it.
Reaching the last item
Before.at() existed, the standard way to reach the end was to work the index out from the length. The count and the last valid index are one apart, so the subtraction has to happen somewhere: on a line of its own, into a variable, or right inside the square brackets. Both ways of reaching the end are common in real code, and both are worth reading fluently. Get the subtraction wrong and the array will not tell you.Growing an array, and changing an item
push adds an item onto the end. An index on the LEFT of an equals sign replaces whatever sat in that slot. Neither one builds a new array; both change the array you already have.The program below keeps a museum's visitor counts for five days. It reads the length, the first item, and the last item, then grows the array and changes one item. Run it, then change it and run it again:
• Add a sixth count to the end of the array on the first line.
• Change
visitors.length - 1 to visitors.length.• Delete the line
visitors.push(97);.let visitors = [56, 79, 94, 69, 85];
console.log(visitors);
console.log("Days:", visitors.length);
console.log("First day:", visitors[0]);
let last = visitors.length - 1;
console.log("Last day:", visitors[last]);
visitors.push(97);
visitors[1] = 75;
console.log(visitors);
console.log("Days now:", visitors.length);
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.