To convert an array to json in JavaScript, you can use the “JSON.stringify()” method. The JSON.stringify() is a built-in method that converts a JavaScript object or value to a JSON string.
Syntax
JSON.stringify(obj, replacer, space)
Arguments
The obj is a required parameter, the value to convert a string.
The replacer is an optional argument with a function or an array to transform the result.
The space is an optional parameter, either a String or a Number.
Return value
The stringify() method returns a JSON string.
Example
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))
Output
After converting into JSON string
["Tokyo","Nairobi","Berlin","Oslo","Moscow"]
Check its data type using typeof()
string
To check the data type of a variable in JavaScript, use the typeof() function.
The moneyheist_json variable is now a string, ready to send to the server.
That’s it for this tutorial.

Niva Shah is a Software Engineer with over eight years of experience. She has developed a strong foundation in computer science principles and a passion for problem-solving.