TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

JavaScript Object assign() Method

JavaScript Object assign() Method with example on javascript, object, javascript assign(), create(), defineProperties(), defineProperty(), entries(), freeze(), javascript tutorial, javascript array etc.

<< Back to JAVASCRIPT

JavaScript Object.assign() Method

The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. Objects are assigned and copied by reference. It will return the target object.

Syntax:

Object.assign(target, sources)

Parameter

target: The target object.

sources: The source object(s).

Return value:

This method returns the target object.

Browser Support:

Chrome Yes
Edge Yes
Firefox Yes
Opera No

Example 1

const object1 = {
  a: 1,
  b: 2,
  c: 3
};
const object3= {
  g: 1,
  h: 2,
  i: 3
};	

const object2 = Object.assign({c: 4, d: 5}, object1);
const object4 = Object.assign({g: 34, h: 25}, object3);
console.log(object2.c, object2.d);
console.log(object4.g, object4.h);

Output:

3 
5
1 
2

Example 2

const object1 = {
  a: 11,
  b: 12,
  c: 33
};

const object2 = Object.assign({c: 4, d: 5}, object1);
console.log(object2.c, object2.d);

Output:

33 
5

Example 3

const object1 = {
  a: 1,
  b: 2,
  c: 3
};

const object2 = Object.assign({a: 3,c: 4, d: 5,g: 23,}, object1);

console.log(object2.c, object2.d,object2.g,object2.a);

Output:

3 
5 
23 
1

Next TopicJavaScript Objects




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf