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.

Example:

// 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))

Output:

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.

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

Leave a Comment