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


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"}

Updated on: 09-Nov-2020

842 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements