The length Property

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — How Many Elements?

You already know `.length` for strings: it counts characters. Arrays have it too, and it counts elements:

const team = ["Aquachu", "Pyrochu", "Florachu"]; console.log(team.length);

Note that `length` is a property, not a method: no parentheses after it. Writing `team.length()` would throw an error.

An empty array has a length of 0, and the count doesn't depend on what the elements contain:

const bag = []; const moves = ["Spark", "Thunder", "Charge", "Zap Cannon"]; console.log(bag.length); console.log(moves.length);

Exercise 2 — The Last Element

Indexes start at 0, so the last index is always one less than the length. With 3 elements, the last index is 2.

const team = ["Aquachu", "Pyrochu", "Florachu"]; console.log(team[team.length - 1]);

This pattern works whatever the size of the array, which is exactly why it's so useful. Without the `- 1`, you'd land one step too far:

const team = ["Aquachu", "Pyrochu", "Florachu"]; console.log(team[team.length]);

Exercise 3 — Quiz: Count Them

What does this code print?

const items = ["Potion", "Potion, Elixir", "Map"]; console.log(items.length);
Options
• 3
• 4
• 2
• 1

Hint : Commas inside quotes are part of a string, not separators between elements.

Exercise 4 — Last One Standing

Print the last Codémon of the team, whatever its size.

Hint : The last index is not equal to the number of elements: think about which direction the gap goes.

Exercise 5 — length in Conditions

Since `length` is a number, you can compare it. A classic: check whether an array is empty before using it.

const bag = []; if (bag.length === 0) { console.log("Your bag is empty!"); } else { console.log("You have " + bag.length + " items"); }

Or check a limit, like a team that can't have more than 6 Codémon:

const team = ["Aquachu", "Pyrochu", "Florachu", "Zap", "Metachu", "Rockachu"]; if (team.length >= 6) { console.log("Team is full!"); } else { console.log("Room for " + (6 - team.length) + " more"); }

Exercise 6 — Quiz: length as an Index

What does this code print?

const levels = [4, 8, 15]; console.log(levels[levels.length]);
Options
• undefined
• 15
• 3
• 8

Hint : Turn the expression inside the brackets into a plain number, then check whether that index exists.

Exercise 7 — Room Left?

Print `Room left` only if the team has fewer than 6 members.

Hint : Translate the condition word for word from the instructions.

Exercise 8 — length Cheat Sheet

Match each expression with its result.

Items to match
• [].length
• ["a", "b", "c"].length
• arr[arr.length - 1]
• arr[arr.length]
Definitions
• 0
• 3
• The last element
• undefined

Exercise 9 — Quiz: Property or Method?

Which line correctly prints the number of elements in `team`?

Options
• console.log(team.length);
• console.log(team.length());
• console.log(length(team));
• console.log(team.size);

Hint : Think about what kind of thing length is: something you call, or something you read?

Exercise 10 — Bag Check

Inspect the bag: 1. Print `"Items: N"` where N is the number of items 2. Print `"First: X"` and `"Last: Y"` with the first and last items 3. If the bag has more than 5 items, print `"Bag is heavy!"`, otherwise `"Bag is light"` Your code must keep working if items are added to the bag.

Starter code
const bag = ["Potion", "Map", "Elixir", "Rope", "Torch", "Berry"]; // Write your code here
Back to the course