JavaScriptObjectsBeginner

Object.assign()

Copies properties from source objects into a target object (mutates target).

Review the syntaxStudy the examplesOpen the coding app
Object.assign(target, ...sources)

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 properties from source objects into a target object (mutates target).

Object.assign() performs a shallow copy of all enumerable own properties from one or more source objects to a target object, returning the modified target. It uses the [[Get]] internal method on sources and [[Set]] on the target, which triggers getters and setters. If sources contain identical keys, later properties overwrite earlier ones. String and Symbol properties are both included in the copy process. A critical edge case involves primitives: null and undefined sources are silently ignored, while other primitives are wrapped in objects. However, only strings result in enumerable properties (indices) being copied. Because Object.assign() mutates the first argument, it is common practice to provide an empty object '{}' as the target to prevent unintended side effects on existing data structures. Performance is generally excellent in modern engines, though it is slightly less performant than object spread syntax in certain V8 scenarios. It does not copy property descriptors (like 'configurable' or 'enumerable') or the prototype chain. IE not supported.

Quick reference

Syntax

Object.assign(target, ...sources)

Inputs

Parameters

targetobject · Object to receive copied properties (will be mutated).
sources (optional)object[] · One or more source objects to copy from.

See it in practice

Examples

1

Merging Multiple Source Objects

const target = { a: 1, b: 1 };
const source1 = { b: 2, c: 2 };
const source2 = { c: 3 };

const result = Object.assign(target, source1, source2);

console.log(target);
console.log(result === target);
Output:
{ "a": 1, "b": 2, "c": 3 } true

Multiple sources are applied in order. Properties from source2 overwrite those from source1, and the target object is mutated and returned.

2

Cloning an Object (Shallow)

const original = { x: 1, y: { z: 2 } };
const clone = Object.assign({}, original);

clone.x = 10;
clone.y.z = 99;

console.log('Original:', original);
console.log('Clone:', clone);
Output:
Original: { "x": 1, "y": { "z": 99 } } Clone: { "x": 10, "y": { "z": 99 } }

Using an empty object as the target creates a shallow copy. Note that nested objects are still passed by reference, so changing a nested property affects both objects.

3

Handling Primitives and Symbols

const sym = Symbol('id');
const target = {};
const result = Object.assign(target, 'foo', { [sym]: 123 }, true);

console.log(result);
Output:
{ "0": "f", "1": "o", "2": "o", [Symbol(id)]: 123 }

Strings are treated as array-like objects with enumerable indices. Booleans and other primitives (except strings) do not have enumerable properties and are effectively ignored during the assign process.

Debug faster

Common Errors

1

LogicError

Cause: Accidentally mutating a source object because it was passed as the first argument (the target).

Fix: Always use an empty object '{}' as the first argument if you want to create a new object without modifying existing ones.

const defaults = { host: 'localhost' };
const config = Object.assign(defaults, { port: 80 }); // defaults is now mutated
2

TypeError

Cause: Attempting to assign properties to a target that has non-writable properties or is sealed/frozen.

Fix: Ensure the target object is extensible and properties are writable, or use object spread to create a new literal.

const target = Object.freeze({ a: 1 });
Object.assign(target, { b: 2 }); // Throws TypeError in strict mode

Runtime support

Compatibility

Node.js, BrowserAll modern browsers, IE not supportedES6 (2015)

Source: MDN Web Docs

Common questions

Frequently Asked Questions

Copies properties from source objects into a target object (mutates target).

target: Object to receive copied properties (will be mutated). sources: One or more source objects to copy from.

LogicError: Always use an empty object '{}' as the first argument if you want to create a new object without modifying existing ones. TypeError: Ensure the target object is extensible and properties are writable, or use object spread to create a new literal.