Let’s see the syntax of JSON.stringify() method.
JSON.stringify(obj, replacer, space)
The obj is a required parameter which is the value to convert a string.
The replacer is an optional argument with either a function or an array used to transform the result.
The space is an optional parameter which is either a String or a Number.
The stringify() method returns a JSON string.
Implementation of JSON.stringify()
Define an array and then convert it into json string.
let moneyheist = ["Tokyo", "Nairobi", "Berlin", "Oslo", "Moscow"] let moneyheist_json = JSON.stringify(moneyheist) console.log("After converting into JSON string") console.log(moneyheist_json) console.log("Check its data type using typeof()") console.log(typeof(moneyheist_json))
After converting into JSON string ["Tokyo","Nairobi","Berlin","Oslo","Moscow"] Check its data type using typeof() string
The moneyheist_json variable is now a string, and it is ready to send to the server.
That’s it for this tutorial.