Slicing, searching, and replacement
String methods are building blocks, not substitutes for a contract. A useful solution names its boundaries and explains whether matching is literal, case-sensitive, and global.
Use half-open slices, recognize search failure values, and distinguish literal, global, and pattern-based replacements.
What to understand
slice(start, end) includes start and excludes end. Negative indices count back from the end, while an omitted end runs to the end of the string. A zero-length slice is valid. Be careful with -0: it is simply zero, so slice(-0) returns the entire string.
indexOf returns -1 when absent and 0 for a match at the beginning. A truthiness check reverses the useful meaning of these values: 0 is falsey while -1 is truthy. Compare explicitly with -1 or use includes when only existence matters.
replace with a string pattern changes the first occurrence. A global regular expression can change every occurrence. Character classes encode exact policies: / +/g means ordinary spaces, while /\s+/g covers additional whitespace. Replacement callbacks are useful when supplied values must be inserted literally, because replacement strings interpret special dollar sequences.
Worked example
const text = " red blue ";
console.assert(text.slice(2, 5) === "red");
console.assert(text.indexOf("red") === 2);
console.assert("red".indexOf("r") === 0);
console.assert("red".indexOf("x") === -1);
console.assert("a b c".replace(/ +/g, "-") === "a-b-c");
console.assert("a a".replace("a", "b") === "b a");
console.assert("tail".slice(-0) === "tail");
console.assert("{x}".replace(/\{x\}/g, () => "$&") === "$&");
The examples keep search positions separate from booleans and make first-only replacement visible. The final callback returns literal $&, whereas a replacement string containing $& would stand for the matched text. This difference matters when rendering user-supplied plain text.
Common mistakes
Do not trim unless the contract permits it. Trimming can silently remove meaningful empty boundary fields. Do not apply a UTF-16 index obtained from the original string to a normalized or transformed string, because its positions may have changed.
Methods may allocate new strings, and ECMAScript does not mandate a particular search algorithm. Avoid claiming a hard linear indexOf guarantee without a specific implementation or algorithm.
Guided practice
Find the final ASCII-space-delimited token without splitting the whole string. Define start and end for " a b ", for all spaces, and for a string containing a tab inside its last token.
Check your reasoning
Skip trailing spaces to find end, then walk backward over nonspaces to find start. For " a b ", the interval is [4,5), producing b. All spaces give an empty interval. Tabs remain ordinary token characters because this contract names only U+0020.
Recap
Use explicit boundaries and failure checks. Know whether your operation replaces one match or all matches, and keep whitespace and coordinate policies visible.
Continue with the linked problems below. Write down the input contract, a small counterexample, and your expected complexity before opening a hint. Run the starter after making one deliberate change, and explain any failing case before changing the code again.