String Indexing and Length

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

This String Indexing and Length worksheet is part of the JavaScript Coding without HTML and CSS journey. Print it as a booklet?

String Indexing and Length

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does word[0] access the first character of a string?
A Yes
B No
2. Does .length give the number of characters in a string?
A Yes
B No
3. Is () used instead of [] to access a character?
A Yes
B No
4. Does .length need parentheses after it, like .length()?
A Yes
B No
5. Is 'Length' with a capital L valid JavaScript syntax?
A No
B Yes

Match the Pairs

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

Left columnRight column
1word[0] Alength
2word.length Bname[1]
3Accesses the second character of a string CWrong - use square brackets, not round brackets
4A property that never needs parentheses DWrong - length is a property, not a method
5console.log(word(0)); EAccesses the first character of the word string
6console.log(word.length()); FGives the total number of characters in word

Definitions

length

Fill in the Blanks

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

1.
console.log(word_0]);
2.
console.log(word.______);
3.
let ch = word[_];
4.
let name = "Amy"; console.log(name.______);
5.
let size = name.______;

Spot the Error

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

Question 1
A console.log(word(0));
B let word = "cat";
C console.log(word.length);
Question 2
A let text = "dog";
B console.log(text(1));
C console.log(text.length);
Question 3
A let text = "cat";
B console.log(text.length);
C console.log(text0]);
Question 4
A let word = "sun";
B console.log(word[0]);
C console.log(word.length);
D console.log(word[1);
Question 5
A let name = "Amy";
B console.log(name[0]);
C console.log(name[1]);
D console.log(name[2]);
E console.log(name.lenght);

Answer Key

String Indexing and Length
Section A: Quick Questions
Q1
A — Yes
Q2
A — Yes
Q3
B — No
Q4
B — No
Q5
A — No
Section B: Match the Pairs
Q1
word[0] → Accesses the first character of the word string
Q2
word.length → Gives the total number of characters in word
Q3
Accesses the second character of a string → name[1]
Q4
A property that never needs parentheses → length
Q5
console.log(word(0)); → Wrong - use square brackets, not round brackets
Q6
console.log(word.length()); → Wrong - length is a property, not a method
Section C: Definitions
Q1
length — The number of characters in a string, accessed as a property without parentheses.
Section D: Fill in the Blanks
Q1
console.log(word[0]);
Q2
console.log(word.length);
Q3
let ch = word[1];
Q4
let name = "Amy"; console.log(name.length);
Q5
let size = name.length;
Section E: Spot the Error
Q1
A — Use square brackets [], not round brackets, to access a character.
Q2
B — Use square brackets [], not round brackets, to access a character.
Q3
C — Missing the opening square bracket before the index.
Q4
D — Missing the closing square bracket after the index.
Q5
E — 'length' is misspelled as 'lenght'.