Interview Q&A · JavaScript · 8 questions in this topic
Strings, Destructuring & Spread — JavaScript Interview Q&A
String mechanics plus the unpacking syntax — destructuring, spread/rest and copies. Part of Arrays, Strings & Collections · 204 questions in the JavaScript bank.
Are strings mutable? How do you manipulate them?
Answer
Strings are immutable primitives — every operation (toUpperCase, slice, replace) returns a new string. Build via concatenation (+), concat, join for arrays, or template literals. Char access via [i]/at() (at() handles negatives).
Immutable-plus-return-new is the string contract.
Example
let s = 'hi'; s.toUpperCase(); // 'HI', s still 'hi' 'hello'.at(-1); // 'o' (negatives work)
How do `split`, `join`, `replace` / `replaceAll` work?
Answer
split divides on string/regex (limit caps parts). join fuses arrays with separators. replace swaps first match (string) or all (regex /g); replaceAll swaps all string matches — plus $patterns (
amp;, $1) and replacer functions for dynamic builds.Regex-or-/g for all-matches is the replace rule.
Example
'a,b,c'.split(','); // ['a','b','c'] 'aaa'.replace('a', 'b'); // 'baa' (first only!) 'aaa'.replaceAll('a', 'b'); // 'bbb'
How does destructuring work (arrays, objects, defaults, nesting)?
Answer
Patterns mirror structure: [a, b] arrays (skippable, rest-collecting), {x, y: z = 1} objects (renaming, defaults for undefined), nestable arbitrarily. Function parameters destructure directly — options objects with defaults are the flagship.
Mirror-shape-plus-defaults is the destructuring model.
Example
function connect({ host = 'localhost', port = 80 } = {}) {} const { a: alpha, b = 2 } = { a: 1 }; // rename + default
💡 Options-object-with-defaults is the pattern to showcase.
How do spread and rest differ in use (calls, literals, params)?
Answer
Spread expands (calls, array/object literals, JSX props). Rest collects (function params, destructuring remainders). Same ... glyph, opposite direction — position decides: call-site/literal (spread) vs parameter/pattern (rest).
Expand-at-use vs collect-at-definition is the directional rule.
Example
const c = [...a, ...b]; // spread: expand const [first, ...rest] = c; // rest: collect f(...args); // spread into call
Shallow vs deep copy with spread — what breaks?
Answer
Spread copies top-level only — nested objects/arrays stay shared, so nested mutation leaks across 'copies'. Deep needs structuredClone (or targeted nested spreads). State-update bugs (React) trace to this constantly.
Nested-sharing is the spread caveat to always voice.
Example
const o = { a: { n: 1 } }; const c = { ...o }; c.a.n = 2; // o.a.n is now 2 (shared!)
How do `padStart/padEnd`, `trim`, `startsWith/endsWith` help?
Answer
padStart/padEnd fixed-width-format (IDs, tables, times). trim/trimStart/trimEnd sanitize input edges. startsWith/endsWith/includes do readable substring checks (no indexOf !== -1 fossils).
Readable-checks-over-indexOf is the modernization note.
Example
'7'.padStart(2, '0'); // '07' ' hi '.trim(); // 'hi' file.endsWith('.js'); // true/false, readable
How do you compare and sort strings (localeCompare, collators)?
Answer
</>/=== compare UTF-16 code units (wrong for accented/case-insensitive needs). localeCompare and Intl.Collator compare linguistically with locale/sensitivity options — sorting names correctly across languages.
Code-units vs linguistic-truth is the comparison split.
Example
'ä'.localeCompare('z', 'de'); // -1 (linguistic, German) // 'ä' > 'z'; // true (code units — wrong linguistically!)
💡 Collator-for-sorting is the i18n-correct answer.
How do you handle Unicode (emoji, surrogate pairs, length)?
Answer
length counts UTF-16 units (emoji = 2); [...str] / Array.from split by code points; Intl.Segmenter splits graphemes (family emoji, flags) correctly. Reversal/slicing must operate per-grapheme, not per-unit.
Units vs code-points vs graphemes is the three-level model.
Example
'😀'.length; // 2 (surrogate pair!) [...'😀'].length; // 1 (code point) // Intl.Segmenter for true graphemes
Recommended resourcesAffiliate links · may earn us a commission