Unveiling JavaScript Object.assign

Table of Contents


JavaScript is a versatile programming language renowned for its ability to manipulate objects and handle complex data structures efficiently. One essential feature for working with objects is Object.assign(). In this article, we’ll delve into the depths of Object.assign() to understand its functionality, applications, and practical usage.

Understanding Object.assign

Object.assign() is a method available in JavaScript, introduced with ECMAScript 2015 (ES6). It is used to copy the values of all enumerable own properties from one or more source objects to a target object. Essentially, it merges multiple objects into a single object, allowing for easy and efficient object composition.


The syntax for Object.assign() is straightforward:

Object.assign(target, ...sources)
  • target: The target object to which properties will be copied.
  • sources: One or more source objects whose properties will be copied to the target object.

Example Usage

Let’s illustrate the usage of Object.assign() with a simple example:

const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };

const merged = Object.assign(target, source);

console.log(merged); // Output: { a: 1, b: 4, c: 5 }
console.log(target); // Output: { a: 1, b: 4, c: 5 }

In this example, the properties from the source object are copied into the target object. If a property exists in both the target and source objects, the value from the source object overwrites the value in the target object.

Cloning Objects

One of the most common use cases for Object.assign() is object cloning. You can create a shallow copy of an object using Object.assign().

const original = { a: 1, b: 2 };
const clone = Object.assign({}, original);

console.log(clone); // Output: { a: 1, b: 2 }

In this example, an empty object {} serves as the target, and original is the source object. This creates a new object with the same properties and values as original.

Merging Objects

Object.assign() is also useful for merging multiple objects into one.

const obj1 = { a: 1 };
const obj2 = { b: 2 };
const obj3 = { c: 3 };

const merged = Object.assign({}, obj1, obj2, obj3);

console.log(merged); // Output: { a: 1, b: 2, c: 3 }

In this example, properties from obj1, obj2, and obj3 are merged into a single object.

Modifying the Target Object

It’s important to note that Object.assign() modifies the target object in place. If you want to avoid mutating the original object, you can pass an empty object {} as the target, as shown in the examples above.


While Object.assign() is powerful, it has some limitations:

  1. Shallow Copy: Object.assign() performs a shallow copy, meaning it only copies enumerable own properties directly from the source objects. If the property values are objects themselves, they are copied by reference, not cloned.
  2. Only Own Properties: Object.assign() copies only the enumerable own properties of the source objects. It does not copy inherited properties or non-enumerable properties.

Deep Cloning Objects

While Object.assign() is excellent for shallow copying objects, it falls short when dealing with nested objects or arrays. In such cases, a deep clone, which creates a new copy of nested objects and arrays, is necessary.

One approach to achieve deep cloning is through recursion combined with Object.assign():

function deepClone(obj) {
  if (obj === null || typeof obj !== 'object') {
    return obj;

  let clone = Array.isArray(obj) ? [] : {};

  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      clone[key] = deepClone(obj[key]);

  return clone;

With this deepClone function, you can create a deep copy of an object, ensuring that nested objects and arrays are also cloned.

const original = {
  a: 1,
  b: {
    c: 2,
    d: [3, 4]

const deepCopy = deepClone(original);

console.log(deepCopy); // Output: { a: 1, b: { c: 2, d: [3, 4] } }

Merging with Default Values

Object.assign() can be used with default values to handle scenarios where a property might be missing in the source object.

const defaults = {
  a: 0,
  b: 1,
  c: 2

const customValues = {
  b: 5,
  d: 10

const merged = Object.assign({}, defaults, customValues);

console.log(merged); // Output: { a: 0, b: 5, c: 2, d: 10 }

In this example, Object.assign() merges the defaults object with customValues. Properties present in customValues overwrite the corresponding properties in defaults, while missing properties are filled in from defaults.

Object.assign() in Functional Programming

In functional programming, Object.assign() can be used to create immutable objects by combining the spread operator (...) with Object.assign().

const initialState = { count: 0 };

function reducer(state, action) {
  switch (action.type) {
    case 'increment':
      return Object.assign({}, state, { count: state.count + 1 });
    case 'decrement':
      return Object.assign({}, state, { count: state.count - 1 });
      return state;

let state = initialState;

// Increment
state = reducer(state, { type: 'increment' });
console.log(state); // Output: { count: 1 }

// Decrement
state = reducer(state, { type: 'decrement' });
console.log(state); // Output: { count: 0 }

In this example, Object.assign() is used within a reducer function to create a new state object with updated properties based on the dispatched action. This approach maintains immutability, which is crucial for predictable state management in applications.

Command PATH Security in Go

Command PATH Security in Go

In the realm of software development, security is paramount. Whether you’re building a small utility or a large-scale application, ensuring that your code is robust

Read More »
Undefined vs Null in JavaScript

Undefined vs Null in JavaScript

JavaScript, as a dynamically-typed language, provides two distinct primitive values to represent the absence of a meaningful value: undefined and null. Although they might seem

Read More »