Skip to content

Object to string JavaScript | Converting example

  • by

Use the JSON stringify() method to convert Object to string in JavaScript. This method converts a JavaScript object or value to a JSON string.

JSON.stringify(obj); 

Quick code

var obj = {
  name: 'myObj'
};

JSON.stringify(obj);

Convert Object to string JavaScript

A simple example code converts the set of the variables in the object to a JSON string. Most modern browsers support this method natively.

<!DOCTYPE html>
<html>
<body>

  <script>

   const obj = {name: "John", age: 70, city: "New York"};
   const res = JSON.stringify(obj); 

   console.log(typeof(obj));
   console.log(res);
   console.log(typeof(res));
 </script>

</body>
</html>

Output:

Object to string JavaScript

Do comment if you have any doubts or suggestions on this Js Object topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading