Skip to main content
InterviewHubPRO
Section Overview
Q198

Question 198 of 536

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

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

What are the applications of the assign method

junior LevelMost AskedCode ExampleEvent LoopPrototypes

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);