String methods & normalization

Make text work for you

Lesson 9 of 20 · 8 min · Module 3: Collections & meaningful data

People type extra spaces and inconsistent capitalization. A small normalization function turns that input into a predictable form.

Start this lesson →

Free. No installation: you write and run the code in your browser, and it is checked automatically.

By the end, you’ll be able to…

  • Apply string methods without changing the original
  • Understand why splitting can create empty values
  • Handle empty input deliberately

The idea

Use trim() to remove outer whitespace, toLowerCase() for lowercase text, split(" ") for words, and join("-") to reconnect them.

const title = "  Small Steps  ";
console.log(title.trim().toLowerCase()); // small steps

01Methods return new values

Strings are immutable. Calling trim does not rewrite the original variable. Use the returned string, either by storing it or by chaining another operation.

02Remove empty pieces

Splitting on one ordinary space creates empty strings when spaces repeat. Filter out those pieces before joining. This exercise is intentionally limited to ordinary spaces, not a full production URL sanitizer.

Your turn

Write slugify(title) to lowercase a title, trim its edges, and replace runs of ordinary spaces with one hyphen. An empty or space-only title returns an empty string.

What the checks look for

  • Normalizes a title
  • Collapses extra ordinary spaces
  • Handles empty text
Try it in the editor →

Take this with you

Normalize input in small, explicit transformations.

Learn JavaScript Foundations for free

20 short lessons with real code and instant checks.

See the whole course →