JavaScript String Methods

Transform Text Without Effort

JavaScript String Methods

The most useful string methods — change case, extract substrings, trim whitespace, replace text.

6 min read Level 1/5 #strings#methods#slice
What you'll learn
  • Change case with `toUpperCase` and `toLowerCase`
  • Extract substrings with `slice`
  • Clean up whitespace with `trim`
  • Replace text with `replace` / `replaceAll`

A method is a function attached to a value. You call methods with dot syntax: value.method(args). Strings come with a bunch of useful ones built in.

Change Case

toUpperCase / toLowerCase script.js
const name = "Ada Lovelace";
console.log(name.toUpperCase()); // "ADA LOVELACE"
console.log(name.toLowerCase()); // "ada lovelace"
▶ Preview: console

Extract a Substring With slice

slice(start, end) returns the part of the string from index start (inclusive) up to end (exclusive). Indices start at 0.

slice positions script.js
const s = "JavaScript";
console.log(s.slice(0, 4));   // "Java"
console.log(s.slice(4));      // "Script"  ← from index 4 to end
console.log(s.slice(-6));     // "Script"  ← last 6 characters
console.log(s.slice(-6, -3)); // "Scr"
▶ Preview: console

Trim Whitespace

User input often has stray spaces around it.

trim script.js
const messy = "   hello world   ";
console.log(messy.trim());      // "hello world"
console.log(messy.trimStart()); // "hello world   "
console.log(messy.trimEnd());   // "   hello world"
▶ Preview: console

Replace Text

replace(needle, replacement) swaps the first occurrence. replaceAll swaps every occurrence.

replace and replaceAll script.js
const s = "I like cats. Cats are great.";

console.log(s.replace("cats", "dogs"));
// "I like dogs. Cats are great."  ← only the first one, case-sensitive

console.log(s.replaceAll("cats", "dogs"));
// "I like dogs. Cats are great."  ← still only one — case still matters

console.log(s.toLowerCase().replaceAll("cats", "dogs"));
// "i like dogs. dogs are great."
▶ Preview: console

Concat / Split / Repeat

A few more you’ll see now and then:

concat, split, repeat script.js
console.log("Hello, ".concat("world!")); // "Hello, world!"
                                          // (prefer + or template literals)

console.log("apple,banana,grape".split(","));
// ["apple", "banana", "grape"]

console.log("ha".repeat(3)); // "hahaha"
▶ Preview: console

A Quick Reference

MethodReturns
toUpperCase()new string, all caps
toLowerCase()new string, all lower
slice(start, end?)substring
trim()string without leading/trailing whitespace
replace(a, b)new string, first ab
replaceAll(a, b)new string, every ab
split(sep)array of parts
repeat(n)string repeated n times

Remember: strings are immutable. Every method returns a new value — it doesn’t change the original.

Try It Yourself

Exercise

Capitalize a name

Difficulty 2/5~3 min
Given the constant `raw = "ada lovelace"`, build a new string `name` that is `"Ada Lovelace"` — first letter of each word capitalized. Log it.
solution.js
const raw = "ada lovelace";
// your code here
2tests will run
💡 Show hint
Split on the space character, capitalize the first letter of each part using `toUpperCase()` and `slice(1)`, and join them back with a space.
✅ Show solution
const raw = "ada lovelace";
const name = raw
  .split(" ")
  .map((w) => w[0].toUpperCase() + w.slice(1))
  .join(" ");
console.log(name);

Up Next

Methods that search a string for a needle.

JavaScript String Search →