Operators and strings · Lesson 8 of 45
String methods
Slice, search, replace and clean up text.
Strings come with built-in methods. These are the ones you'll use every day:
| Method | What it does | Example |
|---|---|---|
| upper/lower case |
|
| removes spaces at both ends |
|
| contains x? |
|
| starts with x? |
|
| position of x (or -1) |
|
| part from a up to b |
|
| splits into an array |
|
| replaces every a with b |
|
| pads to length n |
|
const email = " Ahmed@SeaCode.tech "
const clean = email.trim().toLowerCase()
console.log(clean)
console.log(clean.includes("@"))
console.log(clean.split("@"))
console.log(clean.slice(0, clean.indexOf("@")))
const time = "9:5"
const [h, m] = time.split(":")
console.log(`${h.padStart(2, "0")}:${m.padStart(2, "0")}`)You can chain methods: trim().toLowerCase().
What's the output?
console.log("JavaScript".slice(0, 4))What will this code print?
Complete the code
Split the text at every comma.
const tags = "html,css,js" console.log(tags.(","))
Spot the bug
This code checks the email has an @. Where's the mistake?
Exercise
Reverse "seacode" and print it (edocaes).
Expected output: edocaes
const word = "seacode"
Learn more from
Played 0 of 4 games
Sign in to save your progress and earn XP.