Skip to content

JavaScript object toString() | Method

  • by

Use JavaScript object toString() method to get an object as a string. This method returns a string representing the object.

obj.toString()

The toString() method does not take any parameters.

JavaScript object toString

Simple example code.

<!DOCTYPE html>
<html>
<body>
  <script >

    let num = 100;
    console.log(num.toString(2)); 

    var Student = { Name: 'John',  RollNo: '234' }
    console.log(Student.toString())
  </script>
</body>
</html>

Output:

JavaScript object toString

Converting an object to a string

It recommends using JSON.stringify, which converts the set of the variables in the object to a JSON string. Most modern browsers support this method natively, but for those that don’t, you can include a JS version:

var obj = {
  name: 'myObj'
};

JSON.stringify(obj);

Do comment if you have any doubts or suggestions on this JS object method 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

Your email address will not be published. Required fields are marked *