Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

Object.assign

Object.assign() copies enumerable own properties from one or more sources into a target object. It mutates and returns the target, with later sources winning when keys overlap.

How it works

Object.assign() takes a target followed by zero or more sources. It copies each source's enumerable own properties into the target, in argument order, and returns that same target object; later sources replace earlier values for duplicate keys.

Syntax

Object.assign(target)
Object.assign(target, source1)
Object.assign(target, source1, source2)
Object.assign(target, source1, source2, /* …, */ sourceN)

Parameters

NameTypeDescription
targetanyThe object receiving copied properties; a primitive target is converted to an object.
source1, …, sourceNanyOne or more source objects whose enumerable own properties are applied; null and undefined sources are ignored.

Returns

The modified target object.

Key points

  • It is a static Object method for applying enumerable own properties from multiple sources to one target.
  • A source value replaces an existing target value when both use the same property key.
  • When several sources provide a key, the rightmost source determines the resulting value.
  • The operation supports both string-keyed and symbol-keyed properties, provided they meet the copy rules.
  • The first argument receives the copied properties and is also the object returned by the call.
  • Nullish source arguments are safely ignored during the copy.

Common mistakes

  • Copying can execute source getters and target setters because the operation reads through getters and writes through setters.
  • This is a shallow copy: nested objects are not duplicated, so references remain shared.
  • Null and undefined sources are skipped rather than causing an exception; this differs from an invalid target.
  • A non-writable target property can cause a TypeError when assignment cannot complete.

Example: Copy enumerable properties into a target

const target = { a: 1 };
const source = { b: 2, a: 3 };
const result = Object.assign(target, source);

console.log(result); // { a: 3, b: 2 }
console.log(result === target); // true

The source properties are copied into the target; the later source value replaces the earlier value for the same key.

When to use it

Merge configuration objects

Combine configuration defaults with user-provided overrides while keeping the selected target object.

Extend an existing object

Add enumerable methods or data properties to an existing object or prototype.

Shallow-copy flat data

Create a shallow copy when nested references may intentionally remain shared.

Interview questions

QWhat does Object.assign() copy, and what does it return?

It copies enumerable own properties from each source into the target and returns the modified target. The target is mutated, and later sources take precedence when keys collide.

Follow-up: How would you preserve property descriptors instead? Use Object.getOwnPropertyDescriptor() with Object.defineProperty(), because Object.assign() assigns values rather than copying descriptors.

QHow are primitive targets and absent sources handled?

The target is converted to an object, so a primitive target can be accepted and boxed. With no source arguments, the converted target is returned.

Follow-up: What happens when a source is null or undefined? It is ignored rather than converted or rejected.

QWhy is Object.assign() unsuitable for deep cloning?

Object.assign() performs a shallow copy: an object-valued property contributes its reference, not a recursively cloned object. Use structuredClone() or another deep-cloning approach when independent nested data is required.

Follow-up: Does it copy symbol keys? Yes, enumerable own Symbol properties are copied along with enumerable own string properties.

  • Object.getOwnPropertyDescriptor()
  • Object.defineProperty()
  • structuredClone()
  • Object.create()

Source: Object.assign() - MDN. Checked 2026-10-15.