Convert a JavaScript Object to JSON String

In this tutorial, we are going to learn about how to convert a JavaScript object to a JSON string.

Using JSON.stringify method

In JavaScript, we have a JSON.stringify() method which is used to convert an object into a string.

Let’s see an example.

consider we have an object like this.

const obj = {name:"king",age:10,city:"New York"}

Now we need to convert the above object into a string by using JSON.stringy() method.

console.log(JSON.stringify(obj));

// output --> {"name":"king","age":10,"city":"New York"}