Learning object 3 of 100
String manipulation laboratory
Type a string, choose a method and see the exact value it returns, with every character's index drawn underneath.
Loading…
What it shows
Strings are immutable: every method returns a new value and the original stays the same. Indexes start at 0, so the last character is at length - 1. Methods such as slice take a start index and an end index that is not included.
Guided experiment
- Apply
.trim()to" Ada Lovelace "and compare the length before and after. - Use
.slice(0, 3)and then.slice(-3). What does a negative index mean? - Use
.replaceand then.replaceAllon"banana"with"a". How many letters change each time?
Predict
What does "banana".indexOf("n") return?
Related
Lessons: Lesson 7: Strings and template literals
Projects: Project 9: Shopping Cart Price Calculator
Source: web/assets/js/objects/object-003.js in the companion repository.