These questions ask you to read string literals, interpolation, indexing, slicing, searches, replacement, splitting, and iteration. Before calculating, decide whether the operation works in code units, code points, indexes, or substrings.
Track the representation and the method contract
Strings cannot be changed in place, so string methods produce results without rewriting the source. Single and double quotes create the same kind of value; choose a delimiter that avoids or correctly escapes matching quote characters. Backticks mark a template literal and permit direct line breaks. A placeholder evaluates its expression and converts the result to text: if count is 3, a placeholder expression count * 4 inserts 12. The same placeholder-like characters in an ordinary quoted string stay literal.
| Signal | Procedure and small example |
|---|---|
| length or bracket indexing | Count UTF-16 code units. text[2] reads one code unit at position 2, and an unavailable bracket position produces undefined. |
| slice(start, end) | Include start and stop before end. "garden".slice(2, 5) produces "rde" and leaves the original string unchanged. |
| includes, startsWith, or endsWith | Keep case exactly as written and return a boolean. startsWith checks only the beginning, endsWith only the ending, and includes searches anywhere. |
| replace with a string search value | Change the first match only and return a new string. "ha ha".replace("ha", "ho") produces "ho ha". |
| split(separator) | Return an array of the surrounding substrings. "a|b".split("|") produces ["a", "b"], with the separator omitted. |
| for...of iteration | Process Unicode code points. A supplementary code point stored as a surrogate pair is combined for iteration even though length counts its two code units. |
The tempting wrong routes
- Treating a string as a mutable array overlooks primitive immutability and the UTF-16 representation. A displayed character is not guaranteed to occupy one code unit.
- Choosing a quote style as though it created a different value type invents a semantic difference. Delimiters control parsing and escaping, while backticks add template behavior.
- Expecting an out-of-range bracket read to return an empty string imports the behavior of charAt. Bracket indexing instead produces undefined there.
- Assuming search methods ignore case turns a nonmatch into a match. Compare letter case exactly unless the program explicitly normalizes it.
- Expecting a plain string replacement to change every occurrence overextends replace. With this search form, stop after the first match and preserve the source.
Try it
Classify the statements about expression interpolation in JavaScript strings. Select all that apply.
- A template placeholder evaluates its expression
- An ordinary quoted string interpolates the same way
- The expression result is converted to text
- Interpolation inserts source code without evaluation
Given text = "planet", what does text.slice(1, 4) return, and what happens to text?
- lane, with text unchanged
- lan, with text unchanged
- lan, with text shortened
- pla, with text unchanged
A for...of loop processes a string containing one supplementary Unicode code point encoded as a surrogate pair. How many loop values represent that code point?
- Two, one for each code unit
- One, for the combined code point
- Zero, because it is not basic Unicode
- A runtime-dependent number