JavaScriptLesson 9 of 18

JavaScript String Methods

Strings come with methods that answer questions about text and reshape it. You can check whether a sentence contains a word, split it into a list, replace one word with another, and strip extra spaces. Every one of these is a method call, no regular expressions required.

Strings come with methods that answer questions about text and reshape it. You can check whether a sentence contains a word, split it into a list, replace one word with another, and strip extra spaces. Every one of these is a method call, no regular expressions required.

Two things make string methods easy to keep straight. Strings are immutable, so every method returns a new string and never changes the original. And most methods answer one focused question: includes asks whether something is inside, startsWith asks about the beginning, endsWith asks about the end. Pick the question you want to ask and the method name is usually obvious.

Case and whitespace are the most common cleanup jobs. toLowerCase and toUpperCase change the whole string, and trim removes spaces from both ends. Compare user input with trim then toLowerCase and " ADAM " equals "adam", which is exactly how logins and searches are normalized.

Asking questions about text

includes, startsWith, and endsWith return true or false, and they are the safest way to check text. length counts characters. These replace the old tricks of searching with an index and hoping for the best.

JS
const email = "ada@example.com";
email.includes("@");      // true
email.endsWith(".com"); // true
email.length;           // 16

Cutting text apart

slice takes a portion of a string using start and end positions. split turns a string into an array, and join is the reverse, gluing an array back into a string. They are the standard pair for converting "a,b,c" into a list and back.

JS
const names = "ada,grace,alan";
const list = names.split(",");   // ["ada", "grace", "alan"]
const back = list.join(" + ");  // "ada + grace + alan"
"hello world".slice(0, 5);      // "hello"

Replacing and cleaning

replace swaps the first match for something new, and replaceAll swaps every match. trim, toLowerCase, and toUpperCase clean up the edges and the case. Together they turn messy user input into something your code can compare.

JS
const raw = "  Hello World  ";
raw.trim();                       // "Hello World"
raw.toLowerCase();                // "hello world"
"one two two".replaceAll("two", "3"); // "one 3 3"

Syntax

JS
const input = "  Ada Lovelace  ";
const cleaned = input.trim().toLowerCase();
const firstName = cleaned.split(" ")[0];
console.log(firstName); // "ada"

replace Only Fixes First Match

"cat cat".replace("cat","dog") gives "dog cat" — use replaceAll or /g.

JS
"cat cat".replaceAll("cat","dog")

Good to know

  • Strings are immutable: methods return new strings, they never edit the original.
  • includes, startsWith, and endsWith answer questions about text.
  • split turns text into an array; join turns an array back into text.
  • trim, toLowerCase, and toUpperCase normalize user input for comparisons.
Try it Yourself Edit the code, press Run, then check what changed in the Result.
Ready
Code
Result

Practice makes it stick

Open the Playground and build your own page with the tags you just learned.

→ Open Playground