Using JSON.stringify() to display spread operator result?

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

With Spread Operator, allow the expression to expand to multiple arguments, elements, variables, etc.

You can use JSON.stringify() to convert the JavaScript object to string. Here, we have our object as the result of using spread operator on details1 and details2.


Following is the code −

var details1 = { name: 'John', age: 21 };
var details2 = { countryName: 'US', subjectName:'JavaScript' };
var result= { ...details1, ...details2};

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo267.js.


This will produce the following output on console −

PS C:\Users\Amit\javascript-code> node demo267.js
Updated on 09-Nov-2020 07:46:10