JavaScript AcademyProfessional Engineering

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

  1. Apply .trim() to " Ada Lovelace " and compare the length before and after.
  2. Use .slice(0, 3) and then .slice(-3). What does a negative index mean?
  3. Use .replace and then .replaceAll on "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.