Object

Object.assign

Copies all enumerable own properties from one or more source objects to a target object and returns the modified target object

Syntax

JavaScript
Object.assign(target, ...sources)

Parameters

ParameterTypeDescription
targetobjectThe target object to copy to
sourcesobject[]The source objects to copy from

Return Value

The target object

Examples

Basic Usage
const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
const result = Object.assign(target, source);
console.log(result); // { a: 1, b: 4, c: 5 }
Practical Example
const defaults = { color: 'red', size: 'medium' };
const options = { size: 'large' };
const config = Object.assign({}, defaults, options);
console.log(config); // { color: 'red', size: 'large' }
Advanced Usage
// Spread syntax is often preferred:
const merged = { ...defaults, ...options };

Understanding Object.assign

The Object.assign method in JavaScript copies all enumerable own properties from one or more source objects to a target object and returns the modified target object. It belongs to the Object object and is one of the most widely used methods for working with object values in modern JavaScript and TypeScript applications.

The method signature is Object.assign(target, ...sources). It accepts 2 parameters: target, sources. When called, it returns the target object. Understanding when and how to use assign() helps you write more expressive, readable code.

Common use cases for Object.assign include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like object-create, object-keys, object-fromentries, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for Object.assign is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More Object Methods

Other methods in the Object object

Related Tools

More Object Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.