

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the use of OBJECT.assign() in javascript?
The Object.assign() method is used to copy the values of all of the object's own properties(enumerable only) from one or more source objects to a target object. It will return the target object.
Example
const targetObj = { a: 1, b: 2 }; const sourceObj = { b: 4, c: 5 }; const returnedTarget = Object.assign(targetObj, sourceObj); console.log(targetObj); console.log(returnedTarget); console.log(returnedTarget === targetObj); console.log(sourceObj);
Output
{ a: 1, b: 4, c: 5 } { a: 1, b: 4, c: 5 } true { b: 4, c: 5 }
Note −
sourceObj did not change.
returnedTarget and targetObj are the same.
The Object.assign() method only copies enumerable and own properties from a source object to a target object. It uses [[Get]] on the source and [[Set]] on the target, so it will invoke getters and setters.
- Related Questions & Answers
- Object.assign() in JavaScript?
- Copy objects with Object.assign() in JavaScript
- What is the use of JavaScript cookies?
- What is the use of Map in JavaScript?
- What is the use of Atomics in JavaScript?
- What is the use of window.location in javascript?
- What is the use of sentry in javascript?
- What is the use of JavaScript eval function?
- What is the use of declaring variables in JavaScript?
- What is the use of forEach() method in JavaScript?
- What is the use of Math.pow() method in JavaScript?
- What is the use of substr() method in JavaScript?
- what is the use of slice() method in JavaScript?
- What is the use of Array.entries() method in JavaScript?
- What is the use of charAt() method in JavaScript?
Advertisements