How to Convert an Object to String in JavaScript

To convert object to string in JavaScript we use the inbuilt function JSON.stringify(). This function takes object as an input parameter and returns string equivalent of the given object.


// Convert object to string using JSON.stringify() method
let obj = {
  'name': 'John',
  'age': 23,
  'gender': 'Male'

str_obj = JSON.stringify(obj)

console.log('Type of obj: ',typeof(obj))
console.log('Type of str_obj: ',typeof(str_obj))


Type of obj:  object
Type of str_obj:  string

As you can see from above output, the type of variable obj is an object and the type of variable str_obj is string.

We use JSON.stringify() when we send data to web servers.