How to convert object to a string in JavaScript
In this tutorial, we are going to learn about how to convert a object to a string in JavaScript.
Using the JSON.stringify() method
To convert the object into a string, we can use the built-in JSON.stringify()
method in JavaScript.
Let’s see an example.
consider we have an object like this.
const obj = {name:"king",age:10,city:"New York"};
Now, we are converting the above object into a string by using the JSON.stringfy()
method.
console.log(JSON.stringify(obj));
// output --> {"name":"king","age":10,"city":"New York"}