
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to print content of JavaScript object?
In this tutorial, we will learn to print the content of a JavaScript object.
Objects are similar to variables, but they can contain many values. Javascript object values are written as key: value pairs, and each pair is separated by commas. It is used to access data from databases or any other sources.
Following are the ways to print the content of JavaScript objects −
- Using the JSON.stringify() Method
- And using a for-in loop
- Using Object.values()
Using the JSON.stringify() Method
The JSON.stringify() is used to convert JavaScript Objects into a string. We have to use JSON.stringify() to send the data to the server. Arrays can also be converted to the string by using JSON.stringify().
Syntax
Following is the syntax to print the content of JavaScript objects by using JSON.stringify() −
var obj = {}; JSON.stringify(obj);
Parameters
- obj − Name of an Object
Example
In the below given example, we are using the JSON.stringify() method to print the content of a JavaScript object
<html> <body> <h2> Use <i> JSON.stringify() </i> to print content of JavaScript object </h2> <div id = "div1"> </div> <script> const Student = { name: "Akshay", age: 18, percentage: 95.45 }; const print = JSON.stringify(Student); var message="Content of Javascript object:"; document.getElementById("div1").innerHTML = message+"<br><br>"+print; </script> </body> </html>
In the above example, users can see that we have printed the content of a JavaScript object using the JSON.stringify method.
Using the Object.values() Method
Object.values() is the method used to convert a javascript object into an array. Object.values() takes an Object as a parameter and outputs object values as elements of an array.
Syntax
Following is the syntax to print the content of a JavaScript object −
var obj = {}; Object.values(obj);
Parameters
- obj − Name of an Object
Example
In the below given example, we are using the Object.values() method to print the content of a JavaScript object.
<html> <body> <h3> Use <i> Object.values() </i> to print content of JavaScript object </h3> <div id = "div1"> </div> <script> const Student = { name: "Akshay", age: 18, percentage: 95.45 }; const value = Object.values(Student); var message="Content of Javascript object:"; document.getElementById("div1").innerHTML = message+"<br>"+value; </script> </body> </html>
In the above example, users can see that we have printed the content of a JavaScript object using the Object.Values() method.
Using a for-in loop to print the content of a JavaScript object
To execute a block of statements for every property of an object, a for-in loop is used.
Users can follow the below syntax to print the content of JavaScript objects by using the for-in loop −
Syntax
var object = {}; for (key in object) { // statements }
Parameters
- key − A Name of the property of an object (You can use any user-defined variable)
- object − A Name of an object to print
Example
In the below given example, we are using the for-in loop to print the content of a JavaScript object.
<html> <body> <h3> Use <i> for </i> loop to print content of JavaScript object </h3> <div id = "div1"> </div> <script> const Student = { name: "Akshay", age: 18, percentage: 95.45 }; let value = ""; for (let key in Student) { value += "<br>"+Student[key] ; }; var message="Content of JavaScript object:"; document.getElementById("div1").innerHTML = message+"<br>"+value; </script> </body> </html>
In the above example, users can see that we have printed the content of a JavaScript object using the for-in loop.
We have learned to print the content of a JavaScript Object. Among the above three ways, the JSON.stringify() method prints both keys and their values, whereas the other two ways only print the values of the properties. JSON.stringify() is the most used method for exchanging the data between the client and the server.
- Related Articles
- How to print the content of JavaScript object?
- How to print object array in JavaScript?
- How to print div content using jQuery?
- How to convert a date object's content into json in JavaScript?
- Print JSON nested object in JavaScript?
- How do I display the content of a JavaScript object in a string format?
- How to use JavaScript to replace the content of a document with JavaScript?
- How to print the content into the files using C language?
- How do you print the content of an array in Java?
- How to clear the content of a div using JavaScript?
- Is there a way to print all methods of an object in JavaScript?
- How to get content of div which contains JavaScript script blocks?
- How to Get the Content of an HTML Comment Using JavaScript
- How to add content in section using jQuery/JavaScript?
- How to align text content into center using JavaScript?
