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.

Example

Following is the code −

var details1 = { name: 'John', age: 21 };
var details2 = { countryName: 'US', subjectName:'JavaScript' };
var result= { ...details1, ...details2};
console.log(JSON.stringify(result));

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

node fileName.js.

Here, my file name is demo267.js.

Output

This will produce the following output on console −

PS C:\Users\Amit\javascript-code> node demo267.js
{"name":"John","age":21,"countryName":"US","subjectName":"JavaScript"}
raja
Published on 09-Nov-2020 07:46:10
Advertisements