This example demonstrates common string operations: searching, extracting, replacing, splitting, trimming, padding, and changing case. The code is displayed for study. Run each JavaScript block in a modern browser console or compatible JavaScript runtime.
How can you check whether a sentence contains a word, starts with a phrase, ends with punctuation, and find the positions of matching text?
const sentence = "JavaScript makes the web.";
console.log("contains web:", sentence.includes("web"));
console.log("starts with JavaScript:", sentence.startsWith("JavaScript"));
console.log("ends with period:", sentence.endsWith("."));
console.log("first a:", sentence.indexOf("a"));
console.log("last a:", sentence.lastIndexOf("a"));
console.log("missing word:", sentence.indexOf("Python"));contains web: true
starts with JavaScript: true
ends with period: true
first a: 1
last a: 3
missing word: -1
Use includes() for a yes or no answer, startsWith() and endsWith() for boundaries, and indexOf() or lastIndexOf() when you need an index. A missing search value returns -1, while a match at the start of the string has index 0.
These comparisons are case-sensitive and count indexes in UTF-16 code units.
How can you select part of a string, including a portion counted from the end?
const word = "JavaScript";
console.log("slice by range:", word.slice(4, 10));
console.log("last six units:", word.slice(-6));
console.log("substring:", word.substring(4, 10));
console.log("at from end:", word.at(-1));slice by range: Script
last six units: Script
substring: Script
at from end: t
The end argument is excluded. slice() accepts a negative index, where -1 represents the last UTF-16 code unit. substring() does not interpret a negative index from the end and swaps its indexes if the start is greater than the end.
at() also accepts a negative index, but returns one UTF-16 code unit. For Unicode-aware slicing, use the code-point or grapheme techniques from the previous chapter.
How can you replace the first matching word, replace every matching word, and compute replacement text from a match?
const list = "red, red, red";
console.log("replace first:", list.replace("red", "blue"));
console.log("replace all:", list.replaceAll("red", "blue"));
const price = "Cost: $5";
const doubled = price.replace(/\$(\d+)/, (match, amount) =>
"$" + String(Number(amount) * 2)
);
console.log("computed replacement:", doubled);
console.log("original:", list);replace first: blue, red, red
replace all: blue, blue, blue
computed replacement: Cost: $10
original: red, red, red
With a string search value, replace() changes only the first occurrence and replaceAll() changes every occurrence. Both return new strings.
The callback receives the match and captured groups. It calculates a replacement value for the matched amount. A callback is useful when replacement text is computed dynamically.
How can you split a delimited value, limit the number of results, and see why an empty separator is not Unicode-safe?
const colors = "red,green,blue";
console.log("all colors:", colors.split(",").join(" | "));
console.log("limited colors:", colors.split(",", 2).join(" | "));
const face = "😀";
console.log(
"split empty separator code units:",
face.split("").map((part) => part.charCodeAt(0)).join(", "),
);
console.log(
"string iteration code points:",
[...face].map((part) => part.codePointAt(0)).join(", "),
);all colors: red | green | blue
limited colors: red | green
split empty separator code units: 55357, 56832
string iteration code points: 128512
The optional second argument to split() limits how many array items are returned. With split(""), the emoji is separated into its two UTF-16 surrogate code units. Spread syntax uses the string iterator and keeps the valid surrogate pair together as one code point.
That still does not group grapheme clusters such as combining sequences or joined emoji. Use Intl.Segmenter from the previous chapter when grapheme boundaries matter.
How can you remove whitespace at the edges, add padding, and repeat a string?
const input = " JavaScript \n";
console.log("trim:", input.trim());
console.log("trim start:", JSON.stringify(input.trimStart()));
console.log("trim end:", JSON.stringify(input.trimEnd()));
console.log("pad start:", "7".padStart(3, "0"));
console.log("pad end:", "7".padEnd(3, "."));
console.log("repeat:", "ha".repeat(3));trim: JavaScript
trim start: "JavaScript \n"
trim end: " JavaScript"
pad start: 007
pad end: 7..
repeat: hahaha
trim() removes whitespace from both edges. trimStart() and trimEnd() remove it from one side. The example uses JSON.stringify() so remaining spaces and the newline are visible in the output.
Padding reaches the requested UTF-16 code-unit length. repeat() concatenates the string the requested number of times.
How can you perform a simple case-insensitive substring check?
function includesIgnoringCase(text, query) {
return text.toLowerCase().includes(query.toLowerCase());
}
console.log(includesIgnoringCase("JavaScript", "script"));
console.log(includesIgnoringCase("JavaScript", "Python"));true
false
This approach works for many simple searches. It is not a complete multilingual search solution. Case mappings can depend on locale and may change the number of code points. Use locale-aware behavior when the application's search requirements need it.
How can you clean and transform a string while keeping the original value?
const raw = " JavaScript ";
const cleaned = raw
.trim()
.toLowerCase()
.replaceAll("javascript", "js");
console.log("cleaned:", cleaned);
console.log("original:", raw);cleaned: js
original: JavaScript
Each method returns a new string, and the next method operates on that result. The original raw value remains unchanged.
How can you sort names using a locale's collation rules without modifying the caller's array?
function sortNames(names, locale) {
return [...names].sort((first, second) =>
first.localeCompare(second, locale)
);
}
const names = ["Émile", "Ada", "Zoë"];
const sorted = sortNames(names, "en");
console.log("sorted:", sorted.join(", "));
console.log("original:", names.join(", "));
console.log("apple before banana:", "apple".localeCompare("banana", "en") < 0);sorted: Ada, Émile, Zoë
original: Émile, Ada, Zoë
apple before banana: true
localeCompare() applies locale-aware collation. The exact order can depend on the locale and runtime's collation data. Its result is negative, zero, or positive, so test the sign rather than expecting an exact -1 or 1.
The helper copies the array before sorting. This avoids changing the original array with sort().
How should a browser display user-provided text without treating it as HTML markup?
const userInput = "<strong>Hello</strong>";
const paragraph = document.createElement("p");
paragraph.textContent = userInput;
console.log(paragraph.textContent);
console.log(paragraph.querySelector("strong"));<strong>Hello</strong>
null
Setting textContent creates a text node. The angle brackets are displayed as text, and no strong element is created. String replacement and trimming are not HTML sanitizers.
- Add another occurrence of a to the search sentence and compare indexOf() with lastIndexOf().
- Compare slice() with substring() using a negative index and reversed arguments.
- Replace every occurrence of a chosen word using a callback.
- Use a regular-expression separator to split text on commas or semicolons.
- Compare split(""), spread syntax, and Intl.Segmenter on a flag and a family emoji.
- Change the padding string and observe how the target length works.
- Try localeCompare() with another locale.
- Put text containing markup into textContent and observe that it remains text.
- Search methods are case-sensitive and use UTF-16 indexes.
- slice() and substring() differ in how they handle negative and reversed indexes.
- replace() replaces one string occurrence; replaceAll() replaces all literal occurrences.
- split("") can divide a surrogate pair.
- Trimming affects string edges; padding and repetition produce new strings.
- localeCompare() performs locale-aware comparison, not fixed ASCII sorting.
- JavaScript string methods do not mutate the original string.
- Display untrusted text with textContent instead of interpreting it as HTML.
| Previous chapter | Chapter lesson | Chapter Q&A | Next chapter |
|---|---|---|---|
| 24 - Unicode: code units, code points, and graphemes | Open lesson | Open Q&A | 26 - Operators, precedence, and associativity |