Exerpad
Lesson · Chapter: String Manipulation+10 XP for reading

String Methods

What Are String Methods?

You already know how to create strings and combine them. But strings can do so much more! JavaScript gives you string methods — built-in tools that let you transform, search, and slice text.

To use a method, put a dot . after a string (or a variable holding a string), then the method name with parentheses:

javascript
let message = "hello";
console.log(message.toUpperCase());
Output
Press Run to see the output.
HELLO

Changing Case

Use .toUpperCase() and .toLowerCase() to change all letters to uppercase or lowercase:

javascript
let name = "Alice";
console.log(name.toUpperCase());
console.log(name.toLowerCase());
Output
Press Run to see the output.
ALICE
alice

Cleaning Up Strings

Use .trim() to remove extra spaces from the beginning and end:

javascript
let messy = " hello ";
console.log(messy.trim());
Output
Press Run to see the output.
hello

There's also .trimStart() (start only) and .trimEnd() (end only).

Replacing Text

Use .replaceAll(old, new) to swap every occurrence in a string:

javascript
let sentence = "I like cats";
console.log(sentence.replaceAll("cats", "dogs"));
Output
Press Run to see the output.
I like dogs

Careful: plain .replace() only swaps the first match. Use .replaceAll() when you want them all!

Splitting and Joining

.split(" ") breaks a string into an array of words:

javascript
let sentence = "one two three";
let words = sentence.split(" ");
console.log(words.length);
Output
Press Run to see the output.
3

You can split on any character:

javascript
let data = "red,green,blue";
let colors = data.split(",");
console.log(colors[0]);
Output
Press Run to see the output.
red

.join() does the opposite — it glues an array into one string:

javascript
let words = ["hello", "world"];
let result = words.join(" ");
console.log(result);
Output
Press Run to see the output.
hello world

Searching in Strings

Use .indexOf() to get the position of text inside a string. It returns -1 if not found:

javascript
let message = "hello world";
console.log(message.indexOf("world"));
console.log(message.indexOf("javascript"));
Output
Press Run to see the output.
6
-1

To count how many times something appears, here's a handy trick — split on it and count the pieces:

javascript
let text = "banana";
console.log(text.split("a").length - 1);
Output
Press Run to see the output.
3

Checking Start and End

.startsWith() and .endsWith() return true or false:

javascript
let filename = "photo.png";
console.log(filename.endsWith(".png"));
console.log(filename.startsWith("doc"));
Output
Press Run to see the output.
true
false

Checking What a String Contains

Use .includes() to check if a string contains another string:

javascript
let sentence = "I love JavaScript";
console.log(sentence.includes("JavaScript"));
console.log(sentence.includes("Java "));
Output
Press Run to see the output.
true
false

String Slicing

You can grab parts of a string with .slice(start, end):

javascript
let word = "Python";
console.log(word.slice(0, 3));
console.log(word.slice(2));
console.log(word.slice(0, 4));
Output
Press Run to see the output.
Pyt
thon
Pyth

A negative start counts from the end:

javascript
let word = "Python";
console.log(word.slice(-3));
Output
Press Run to see the output.
hon

To reverse a string, split it into characters, reverse the array, and join it back:

javascript
let word = "hello";
console.log(word.split("").reverse().join(""));
Output
Press Run to see the output.
olleh

Chaining Methods

You can chain methods together — each one works on the result of the previous one:

javascript
let messy = " Hello World ";
let clean = messy.trim().toLowerCase().replaceAll(" ", "-");
console.log(clean);
Output
Press Run to see the output.
hello-world