Print contents of an object in JavaScript
This post will discuss how to print the contents of an object in JavaScript.
1. Using Window.alert() function
The Window.alert() method displays a dialog with the specified content.
Printing an object using the Window.alert() method will display [object Object] as the output. To get the proper string representation of the object, the idea is to convert the object into a string first using the JSON.stringify() method.
To pretty-print it, specify the total number of space characters to use as white space in the third argument with the second argument undefined.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
var obj = [ { name: 'Max', age: 23 }, { name: 'John', age: 20 }, { name: 'Caley', age: 18 } ]; alert(JSON.stringify(obj, null, 4)); /* Output: [ { "name": "Max", "age": 23 }, { "name": "John", "age": 20 }, { "name": "Caley", "age": 18 } ] */ |
2. Using console.log() function
The console.log() is a standard method to print a message to the web console. This method is often used for debugging without irritating alerts and can be used to print an object’s contents.
|
1 2 3 4 5 6 |
var obj = { name: 'Max', age: 23, sex: 'Male' }; console.log(obj); /* Output: { name: 'Max', age: 23, sex: 'Male' } */ |
MDN recommends using console.log(JSON.parse(JSON.stringify(obj))) over console.log(obj) for printing objects. This is done to avoid constant updates to the console whenever the value of the object changes.
This method won’t work if you try to concat a string with the object, as shown below:
|
1 2 3 4 5 6 |
var obj = { name: 'Max', age: 23, sex: 'Male' }; console.log('Value : ' + obj); /* Output: Value : [object Object] */ |
The correct way is to pass the message as the first parameter and the object as the second parameter to the log method.
|
1 2 3 4 5 6 |
var obj = { name: 'Max', age: 23, sex: 'Male' }; console.log('Value :', obj); /* Output: Value : { name: 'Max', age: 23, sex: 'Male' } */ |
3. Using console.dir() function
The console.dir() displays all the properties of the specified JavaScript object in the web console.
|
1 2 3 4 5 6 |
var obj = { name: 'Max', age: 23, sex: 'Male' }; console.dir(obj); /* Output: { name: 'Max', age: 23, sex: 'Male' } */ |
The actual difference between console.dir() and console.log() comes when printing the DOM elements to the console. The console.log gives special treatment to DOM elements and prints the element in an HTML-like tree, whereas console.dir prints the element in a JSON-like tree.
With the Firefox browser, you can use the non-standard method Object.toSource() to print objects using any of the above-mentioned methods.
That’s all about printing the contents of an object in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)