JavaScript Spread Operator Interview Questions and Answers
Understand how spread expands iterable values and object properties for copying and composition.
Frequently Asked Questions
Important questions are highlighted in the sections below; select one to jump to its answer.
- How to join multiple arrays using the spread operator?
- How to pass arguments in a function using the spread operator?
- How to do a shallow copy using the spread operator?
- Split a string into Unicode characters with spread syntax.
- What does spread syntax do with an iterable?
- Does array spread create a deep copy?
- Which properties does object spread copy?
Theoretical JavaScript Interview Questions
The spread operator (...) unpacks the elements of an iterable - array, string, or object - into individual elements. It's hugely versatile: spread an array into function arguments, merge arrays with [...arr1, ...arr2], or clone/merge objects with {...obj1, ...obj2}. Just remember it's a shallow copy - nested objects are still references.
For arrays use [...original], for objects use {...original} - both give you a new top-level container with the same values. The gotcha is that this is only one level deep - if your array contains objects or your object has nested objects, those inner values are still shared references. For a true deep clone, reach for structuredClone() or JSON.parse(JSON.stringify(obj)).
Just spread each array inside a new array literal: const combined = [...arr1, ...arr2, ...arr3]. You can mix in individual values too - like [0, ...arr1, ...arr2, 99]. It's cleaner than concat() and handles as many arrays as you need.
Spread an array directly at the call site: myFunction(...args). So if args = [1, 2, 3], that's the same as myFunction(1, 2, 3) - no more .apply() gymnastics. A classic use case is Math.max(...numbers) since Math.max doesn't accept an array, just individual arguments.
Optional chaining (?.) lets you safely access deeply nested properties without manually checking each level for null or undefined. Instead of writing if (user && user.address && user.address.city), you just write user?.address?.city. If any part of the chain is null or undefined, the whole expression returns undefined instead of throwing a TypeError. This was a game-changer for working with API responses.
Spread syntax expands an iterable's values into an array literal, function arguments, or another supported context. It consumes the iterable, so a custom iterator can have side effects.
No. `[...array]` creates a new outer array but preserves references to nested objects and arrays. It is a shallow copy.
Object spread copies own enumerable string and symbol properties into a new object, reading their values. It does not preserve the source prototype or copy inherited and non-enumerable properties.
When later spreads or properties provide the same key, the later value wins. Put explicit overrides after a spread when they should take precedence.
Array and argument spread consume an iterable through its iterator, whereas object spread copies enumerable own properties from an object. An object need not be iterable to use object spread.
A string is iterable by Unicode code point, so array spread produces its iterated string values rather than indexing UTF-16 code units. This can differ from `string.split("")` for characters represented by surrogate pairs.
Yes. Copying an enumerable own accessor reads its value, so the getter runs; the new object's property is a data property containing that value rather than the original accessor.
Engines impose practical limits on the number of function arguments, so `fn(...veryLargeArray)` can exceed that limit. Process large collections in chunks or pass the collection as one argument.
JavaScript Practical and Coding Interview Questions
Problem: Use the spread operator to convert a string containing an emoji into an array of characters.
const characters = [...'Hi 🙂'];
console.log(characters);
console.log(characters.length);Problem: Merge default and user settings so user values override defaults, then force `enabled` to remain true. Create a new settings object and show that the original defaults are unchanged.
const defaults = { theme: "light", enabled: false, retries: 2 };
const userSettings = { theme: "dark", retries: 5 };
const settings = { ...defaults, ...userSettings, enabled: true };
console.log(settings);
console.log(defaults);