String Methods

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

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

String Methods

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does toUpperCase() return a new string in all caps?
A Yes
B No
2. Does toLowerCase() change the original string?
A No
B Yes
3. Does trim() remove whitespace from the start and end?
A Yes
B No
4. Is 'touppercase' with no capitals valid JavaScript syntax?
A Yes
B No
5. Do these string methods need parentheses to be called?
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
1name.toUpperCase() Ainput.trim()
2name.trim() Btext.toLowerCase()
3Converts every letter of a string to lowercase CReturns name in all uppercase letters
4Removes leading and trailing whitespace from a string DReturns name with whitespace removed from both ends
5text.touppercase() EWrong - method names are camelCase: toUpperCase
6text.Trim() FWrong - 'trim' must start with a lowercase t

Definitions

toUpperCase()

Fill in the Blanks

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

1.
console.log(name.___________());
2.
console.log(name.___________());
3.
console.log(name.____());
4.
let upper = word.___________();
5.
let trimmed = input.____();

Spot the Error

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

Question 1
A let a = " hi ";
B console.log(a.Trim());
C console.log(a);
Question 2
A let b = "Hi";
B console.log(b.touppercase());
C console.log(b);
Question 3
A let f = "Hi";
B console.log(f);
C console.log(f.toUpperCase);
Question 4
A console.log(g.trim);
B let g = " hi ";
C console.log(g);
Question 5
A let h = "Hi";
B console.log(h.toUpperCase());
C console.log(h);
D console.log(h.touppercase());

Answer Key

String Methods
Section A: Quick Questions
Q1
A — Yes
Q2
A — No
Q3
A — Yes
Q4
B — No
Q5
B — Yes
Section B: Match the Pairs
Q1
name.toUpperCase() → Returns name in all uppercase letters
Q2
name.trim() → Returns name with whitespace removed from both ends
Q3
Converts every letter of a string to lowercase → text.toLowerCase()
Q4
Removes leading and trailing whitespace from a string → input.trim()
Q5
text.touppercase() → Wrong - method names are camelCase: toUpperCase
Q6
text.Trim() → Wrong - 'trim' must start with a lowercase t
Section C: Definitions
Q1
toUpperCase() — Returns a new string with all letters converted to uppercase.
Section D: Fill in the Blanks
Q1
console.log(name.toUpperCase());
Q2
console.log(name.toLowerCase());
Q3
console.log(name.trim());
Q4
let upper = word.toUpperCase();
Q5
let trimmed = input.trim();
Section E: Spot the Error
Q1
B — 'trim' must be lowercase - a capital T causes an error.
Q2
B — Method names are camelCase - it should be toUpperCase, not touppercase.
Q3
C — Missing parentheses - toUpperCase() needs to be called.
Q4
A — Missing parentheses - trim() needs to be called.
Q5
D — Method names are camelCase - it should be toUpperCase, not touppercase.