The array length property

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

The array length property

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. let pets = ['cat', 'dog', 'fish']; Which gives the number of items in pets?
A pets.length
B pets.length()
2. let scores = [10, 20, 30, 40]; Which expression gets the LAST item?
A scores[scores.length - 1]
B scores[scores.length]
3. What does this print? let list = []; console.log(list.length);
A 0
B undefined
4. let games = ['chess', 'tag']; games.push('soccer'); What is games.length now?
A 2
B 3
5. An array has 5 items. What is the index of its last item?
A 5
B 4

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1scores[scores.length - 1] AThe last index is length - 1, so this reads past the end and gives undefined. Write colours[colours.length - 1].
2let items = []; console.log(items.length); Blet count = names.length;
3length is a property, so it is written without brackets to give the number of items Clength is a property, not a method, so it must not have brackets. Write fruits.length.
4The length grows when an item is added to the array DGets the last item in the scores array, because the last index is always length - 1
5let total = fruits.length(); Elet pets = ['cat']; pets.push('dog'); console.log(pets.length);
6let last = colours[colours.length]; FPrints 0, because an empty array has a length of 0

Definitions

array.length

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
let scores = [5, 8, 9]; console.log(scores.______);
2.
let scores = [5, 8, 9]; console.log(scores[scores.length - _]);
3.
let list = []; console.log(list.length === _);
4.
let pets = ["cat"]; pets.push("dog"); console.log(pets.length === _);
5.
let nums = [4, 7, 2]; for (let i = 0; i _ nums.length; i++) console.log(nums[i]);

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A let items = {};
B items.push('pen');
C items.push('ruler');
D console.log(items.length);
Question 2
A function lastItem(list) {
B return arr[arr.length - 1];
C }
D console.log(lastItem([4, 8]));
Question 3
A const days = ['Mon', 'Tue'];
B days.push('Wed');
C console.log(days.length());
Question 4
A const a = [5, 10, 15];
B const last = a[a.length];
C console.log(last);
Question 5
A const queue = [];
B queue.push('Sam');
C console.log(queue.lenght);

Answer Key

The array length property
Section A: Quick Questions
Q1
A — pets.length
Q2
A — scores[scores.length - 1]
Q3
A — 0
Q4
B — 3
Q5
B — 4
Section B: Match the Pairs
Q1
scores[scores.length - 1] → Gets the last item in the scores array, because the last index is always length - 1
Q2
let items = []; console.log(items.length); → Prints 0, because an empty array has a length of 0
Q3
length is a property, so it is written without brackets to give the number of items → let count = names.length;
Q4
The length grows when an item is added to the array → let pets = ['cat']; pets.push('dog'); console.log(pets.length);
Q5
let total = fruits.length(); → length is a property, not a method, so it must not have brackets. Write fruits.length.
Q6
let last = colours[colours.length]; → The last index is length - 1, so this reads past the end and gives undefined. Write colours[colours.length - 1].
Section C: Definitions
Q1
array.length — A property (no brackets) giving how many items are in an array. The last index is length - 1, an empty array has length 0, and length grows as items are added.
Section D: Fill in the Blanks
Q1
let scores = [5, 8, 9]; console.log(scores.length);
Q2
let scores = [5, 8, 9]; console.log(scores[scores.length - 1]);
Q3
let list = []; console.log(list.length === 0);
Q4
let pets = ["cat"]; pets.push("dog"); console.log(pets.length === 2);
Q5
let nums = [4, 7, 2]; for (let i = 0; i < nums.length; i++) console.log(nums[i]);
Section E: Spot the Error
Q1
A — {} makes an empty object, not an array, so push() fails and items.length is undefined. An empty array is written [] and starts with length 0.
Q2
A — The parameter is named list, but the body uses arr, so arr is not defined. The parameter must be arr to match arr.length.
Q3
C — length is a property, not a method, so it has no brackets. It should be days.length, which gives 3.
Q4
B — The last index is length - 1 because indexes start at 0. a[a.length] reads past the end and gives undefined. It should be a[a.length - 1].
Q5
C — length is misspelled as lenght, so it gives undefined. It should be queue.length, which prints 1 after one push.