Spread operator for objects (...)
Copies/merges objects immutably using the spread operator.
const merged = { ...a, ...b };
const updated = { ...obj, key: newValue };This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Copies/merges objects immutably using the spread operator.
The object spread operator, introduced in ES2018, allows for the creation of a new object literal by copying enumerable own properties from a source object. It is a declarative alternative to Object.assign(). Technically, it performs a shallow copy: while top-level primitive values are duplicated, any nested objects or arrays are copied by reference. Performance is typically O(N) where N is the number of properties being copied. Developers should be aware that spread does not copy inherited properties (from the prototype chain) or non-enumerable properties. When merging multiple objects, the property order is significant; if multiple objects share the same key, the value from the last object spread (or the last key explicitly defined) will overwrite previous values. IE is not supported.
Quick reference
Syntax
const merged = { ...a, ...b };
const updated = { ...obj, key: newValue };
See it in practice
Examples
Merging and Overriding Properties
const defaults = { theme: 'light', showSidebar: true };
const userSettings = { theme: 'dark' };
const finalConfig = { ...defaults, ...userSettings };{ "theme": "dark", "showSidebar": true }
Properties from userSettings overwrite those in defaults because userSettings is spread later in the object literal.
Immutable State Update
const profile = { id: 1, username: 'dev_user', role: 'guest' };
const updatedProfile = { ...profile, role: 'admin' };{ "id": 1, "username": "dev_user", "role": "admin" }
Commonly used in React or Redux to return a new object reference with specific property modifications without mutating the original state.
The Shallow Copy Effect
const original = { name: 'App', meta: { version: '1.0' } };
const copy = { ...original };
copy.meta.version = '2.0';
console.log(original.meta.version);"2.0"
Because spread performs a shallow copy, the 'meta' object is shared by reference. Updating the copy's nested property affects the original object.
Debug faster
Common Errors
LogicError
Cause: Placing the spread operator after an explicit property definition, causing the explicit property to be overwritten by the spread object's defaults.
Fix: Ensure the spread operator comes before any specific property overrides.
const user = { name: 'Alice', ...{ name: 'Default' } }; // Result: name is 'Default'LogicError
Cause: Assuming spread creates a deep clone of nested objects or arrays.
Fix: For deep nesting, spread each level individually or use structuredClone() for a true deep copy.
const obj = { a: { b: 1 } };
const copy = { ...obj }; // copy.a is the same reference as obj.aRuntime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Copies/merges objects immutably using the spread operator.
LogicError: Ensure the spread operator comes before any specific property overrides. LogicError: For deep nesting, spread each level individually or use structuredClone() for a true deep copy.