JavaScript String Methods
You can already write a string. This session is about methods: small tools you call on that text to change case, cut a piece, clean spaces, swap words, or ask “does this phrase appear inside?” Deeper search patterns and every method in the language can wait — start with a short everyday set.
How do string methods work in JavaScript?
A string method is a function attached to a string value. You call it with a dot, the method name, and parentheses: "hi".toUpperCase(). Most of these tools return a new string. The original stays as it was unless you store the result (let clean = raw.trim()). Common starters: toUpperCase / toLowerCase for case, slice to cut a piece, trim to drop edge spaces, replace to swap one match, includes for a yes/no search, and indexOf for a starting position (or -1 if missing).
String method toolkit
📊 Everyday string methods beginners use first
| Idea | Example | Result / note |
|---|---|---|
| Uppercase | "hi".toUpperCase() |
"HI" |
slice |
"JavaScript".slice(0, 4) |
"Java" (end index not included) |
trim |
" hi ".trim() |
"hi" |
replace |
"a-a".replace("-", "/") |
"a/a" (first match) |
includes |
"hello".includes("ell") |
true |
indexOf |
"hello".indexOf("l") |
2 (first index; -1 if missing) |
Change letter case
toUpperCase() and toLowerCase() build a new string with all letters upper or lower. Handy before comparing text the user typed.
let city = "Tehran";
console.log(city.toUpperCase());
console.log(city.toLowerCase());
console.log(city);
Cut a piece with slice
slice(start, end) copies from start up to (but not including) end. Omit end to go through the last character. Indexes start at 0.
let word = "JavaScript";
console.log(word.slice(0, 4));
console.log(word.slice(4));
console.log(word);
Clean edge spaces with trim
User input often has spaces at the start or end. trim() removes those edge spaces and returns a cleaner string. Spaces in the middle stay.
let raw = " hello ";
let clean = raw.trim();
console.log(raw.length);
console.log(clean.length);
console.log(clean);
Swap text with replace
replace(old, new) swaps the first match of old with new. The rest of the string stays. (Replacing every match is a later topic.)
let line = "red-blue-red";
console.log(line.replace("-", "/"));
console.log(line);
Search with includes and indexOf
includes(piece) answers yes/no (true / false). indexOf(piece) returns the index of the first match, or -1 if the piece is not found.
let msg = "Welcome guest";
console.log(msg.includes("guest"));
console.log(msg.includes("admin"));
console.log(msg.indexOf("come"));
console.log(msg.indexOf("x"));
Clean a title on the page
Trim input, fix case, then put the result on the page with textContent.
<!DOCTYPE html>
<html>
<head>
<title>String methods — clean title</title>
</head>
<body>
<p id="out">…</p>
<script>
let raw = " javaScript methods ";
let title = raw.trim().toUpperCase();
document.getElementById("out").textContent = title;
</script>
</body>
</html>
The paragraph shows the cleaned title. Change only raw, refresh, and confirm trim + case both applied.
Try it yourself
- Log
"Tehran".toUpperCase()and"Tehran".toLowerCase(); also log the original variable if you store it first. - From
"JavaScript",slicethe first four characters, thenslicefrom index4to the end. - Trim
" hi "and compare.lengthbefore and after. - On
"a-b-c",replacethe first"-"with"/"and log both the result and the original. - On a short sentence, check
includesfor a word that is present and one that is not; logindexOffor a letter. - In the HTML demo, change only
raw, refresh, and read the new title.
Common mistakes
- Writing
toUpperCasewithout()— you need a call:.toUpperCase() - Expecting
"hi".toUpperCase()to change the original string — it returns a new one; assign if you need it - Thinking
slice(0, 4)includes index4— the end index is excluded - Assuming
replacechanges every match — by default it changes the first match only - Using only
indexOfwhen you need a clear yes/no —includesis easier to read for that
Summary
- Call methods with
.+ name +() toUpperCase/toLowerCasechange case;slicecuts a piece;trimcleans edgesreplaceswaps the first match;includes/indexOfsearch inside the string- Methods usually return a new value — store it when you want to keep the result
- Next: Booleans —
trueandfalsefor yes/no decisions
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top