InterviewHubPRO
Section Overview
Q198

Question 198 of 521

Part 4: Prototypes, Object Methods & Event Loop (174 - 253)

38%
JavaScript Internals (521 Qs)•Part 4: Prototypes, Object Methods & Event Loop (174 - 253)

What are the applications of the assign method

Below are the some of main applications of Object.assign() method,

  1. It is used for cloning an object.
  2. It is used to merge objects with the same properties.

Practical Code Example

javascript
// Applications of Object.assign():
// 1. Merging objects:
const defaults = { theme: 'light', fontSize: 14 };
const userPrefs = { theme: 'dark' };
const merged = Object.assign({}, defaults, userPrefs);
console.log(merged); // { theme: 'dark', fontSize: 14 }

// 2. Cloning an object (shallow copy):
const clone = Object.assign({}, merged);