Interview Q&A · JavaScript · 8 questions in this topic

Strings, Destructuring & Spread — JavaScript Interview Q&A

All JavaScript topics

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

Example.jsjs
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

Example.jsjs
'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

Example.jsjs
function connect({ host = 'localhost', port = 80 } = {}) {}
const { a: alpha, b = 2 } = { a: 1 }; // rename + default

💡 Options-object-with-defaults is the pattern to showcase.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits