Spread syntax

Also known as: spread operator

On this page 7

Spread syntax, often called the spread operator, is the JavaScript notation ... that expands an iterable or an object into its individual parts inside an array literal, a function call or an object literal. In browser JavaScript, it is the standard way to copy, merge and pass collections without mutating the original data, and it appears throughout modern web application code.

Spread syntax was introduced for arrays and function calls in ECMAScript 2015 and extended to object literals in ECMAScript 2018. Strictly speaking it is syntax, not an operator, because ...value cannot stand alone as an expression; it only works in the three contexts the language defines.

How spread syntax expands values

Spread syntax works by iterating the source value and inserting each produced element at the position of the three dots. In array literals and function calls, the engine calls the value’s Symbol.iterator method, so any iterable works: arrays, strings, Map, Set, NodeList and generator results.

In object literals, the mechanism is different: the engine copies the source object’s own enumerable properties, including symbol keys, as if by assignment of each key in turn. Prototype properties and non-enumerable properties are not copied, and accessor definitions are lost: getters are invoked and only their current values are stored. Class instances therefore lose their methods and become plain data objects after spreading.

ContextExampleWhat is expandedRequirement
Array literal[...a, ...b]Elements of an iterableValue must be iterable
Function callMath.max(...nums)Elements as separate argumentsValue must be iterable
Object literal{...defaults, ...opts}Own enumerable propertiesAny value; null is ignored
The three contexts where spread syntax is valid

Spreading a plain object into an array, as in [...{a: 1}], throws a TypeError because ordinary objects are not iterable. Spreading null or undefined into an object literal, by contrast, is silently ignored, which is convenient for optional configuration.

Spread syntax compared with rest and older techniques

Spread syntax and rest syntax use the same three dots but do opposite jobs: spread expands one value into many, while rest collects many values into one. The position decides which is meant: rest appears in parameter lists and destructuring patterns, spread in literals and calls.

TechniqueDirectionTypical useNote
Spread syntaxMany out of oneCopy, merge, pass argumentsCreates new arrays or objects
Rest parametersOne out of manyVariable-length function argumentsMust be the last parameter
Destructuring assignment with restOne out of manyExtract some keys, keep the restPairs naturally with spread
Object.assign()Merge into targetMerging objectsMutates the target and triggers setters
Function.prototype.apply()Array to argumentsLegacy argument passingLargely replaced by spread
Spread syntax and related approaches

A subtle difference from Object.assign() is that spread defines new properties on a fresh object instead of assigning them to an existing one, so setters on the target are never called and the result is always a new plain object.

Common patterns in browser code

Spread syntax is most valuable wherever code must produce a new value instead of changing an old one, which is the core rule of state management in many frontend frameworks. The patterns below are the ones that appear most often.

PatternExamplePurpose
Shallow copy of an arrayconst copy = [...items]Avoid mutating shared data
Immutable state update{...state, loading: false}Change one field in a new object
Merging defaults{...defaults, ...userOptions}Later keys override earlier ones
Converting a NodeList[...document.querySelectorAll('li')]Use array methods on DOM results
Removing duplicates[...new Set(tags)]Unique values in insertion order
Conditional property{...(isAdmin && {role: 'admin'})}Add a key only when true
Everyday spread patterns in the browser

Order matters in object spread: when keys collide, the last occurrence wins. Writing {...userOptions, ...defaults} instead of the reverse is a classic bug that silently discards the user’s settings.

Shallow copies and performance limits

Spread syntax creates only a shallow copy: the top-level container is new, but nested arrays and objects are shared by reference. Modifying copy.user.name therefore also changes the original, which surprises developers who expect a full clone.

For a true deep copy, the browser provides structuredClone(), which handles nested structures, dates, maps and circular references, although it cannot clone functions or DOM nodes. Spread remains the right tool when only the outer level must change.

SymptomCauseRemedy
Original object changes unexpectedlyNested data shared by shallow copySpread each changed level or use structuredClone()
RangeError in a function callSpreading a very large array as argumentsUse a loop or reduce instead
Slow rendering in large listsCopying big arrays on every updateBatch updates or use mutable structures locally
TypeError: not iterableSpreading a plain object into an arrayUse Object.values() or Object.entries()
Problems, causes and remedies

Each spread allocates a new array or object and copies every element, so the cost is linear in size. Inside loops this can turn an algorithm from linear to quadratic, for example when an accumulator array is re-spread on every iteration of reduce.

Browser support and transpilation

Array and call spread are supported in all current evergreen browsers, and object spread has been available in them since about 2018. Only projects that still target legacy engines such as Internet Explorer 11 need a build step.

Transpilers such as Babel and TypeScript rewrite spread into helper calls or concat and Object.assign equivalents. The output is functionally close but not identical: loose transpilation modes may skip iterator semantics, which changes results for Set, Map and generator sources.

The short version

Spread syntax is three dots that tell JavaScript to unpack a collection into its separate pieces. Imagine emptying a bag of groceries onto a table: the items are the same, but now each one lies on its own and can be arranged with other items.

Programmers use it to make copies and combine lists or settings without damaging the originals. It is short, easy to read and works in every modern browser, but it only copies the top layer, not the items packed inside.

Where it makes a difference

A front-end developer builds a shopping cart for an online store. Each time a customer changes a quantity, the code creates the next state with {...cart, items: cart.items.map(...)}, replacing only the changed item and keeping every other entry intact. Because the old state is never mutated, the framework detects the change by a simple reference comparison and re-renders only the affected row.

Earlier, the same component used direct mutation, and the quantity badge sometimes failed to update. Switching to spread-based immutable updates removed the stale display bug and made the undo feature trivial: the previous cart object is still available, unchanged, and can be restored with a single assignment.