- Javascript Basics Tutorial
- Javascript - Home
- JavaScript - Overview
- JavaScript - Features
- JavaScript - Enabling
- JavaScript - Placement
- JavaScript - Syntax
- JavaScript - Hello World
- JavaScript - Console.log()
- JavaScript - Comments
- JavaScript - Variables
- JavaScript - let Statement
- JavaScript - Constants
- JavaScript - Data Types
- JavaScript - Type Conversions
- JavaScript - Strict Mode
- JavaScript - Reserved Keywords
- JavaScript Operators
- JavaScript - Operators
- JavaScript - Arithmetic Operators
- JavaScript - Comparison Operators
- JavaScript - Logical Operators
- JavaScript - Bitwise Operators
- JavaScript - Assignment Operators
- JavaScript - Conditional Operators
- JavaScript - typeof Operator
- JavaScript - Nullish Coalescing Operator
- JavaScript - Delete Operator
- JavaScript - Comma Operator
- JavaScript - Grouping Operator
- JavaScript - Yield Operator
- JavaScript - Spread Operator
- JavaScript - Exponentiation Operator
- JavaScript - Operator Precedence
- JavaScript Control Flow
- JavaScript - If...Else
- JavaScript - While Loop
- JavaScript - For Loop
- JavaScript - For...in
- Javascript - For...of
- JavaScript - Loop Control
- JavaScript - Break Statement
- JavaScript - Continue Statement
- JavaScript - Switch Case
- JavaScript - User Defined Iterators
- JavaScript Functions
- JavaScript - Functions
- JavaScript - Function Expressions
- JavaScript - Function Parameters
- JavaScript - Default Parameters
- JavaScript - Function() Constructor
- JavaScript - Function Hoisting
- JavaScript - Self-Invoking Functions
- JavaScript - Arrow Functions
- JavaScript - Function Invocation
- JavaScript - Function call()
- JavaScript - Function apply()
- JavaScript - Function bind()
- JavaScript - Closures
- JavaScript - Variable Scope
- JavaScript - Global Variables
- JavaScript - Smart Function Parameters
- JavaScript Objects
- JavaScript - Number
- JavaScript - Boolean
- JavaScript - Strings
- JavaScript - Arrays
- JavaScript - Date
- JavaScript - DataView
- JavaScript - Math
- JavaScript - RegExp
- JavaScript - Symbol
- JavaScript - Sets
- JavaScript - WeakSet
- JavaScript - Maps
- JavaScript - WeakMap
- JavaScript - Iterables
- JavaScript - Reflect
- JavaScript - TypedArray
- JavaScript - Template Literals
- JavaScript - Tagged Templates
- Object Oriented JavaScript
- JavaScript - Objects
- JavaScript - Classes
- JavaScript - Object Properties
- JavaScript - Object Methods
- JavaScript - Static Methods
- JavaScript - Display Objects
- JavaScript - Object Accessors
- JavaScript - Object Constructors
- JavaScript - Native Prototypes
- JavaScript - ES5 Object Methods
- JavaScript - Encapsulation
- JavaScript - Inheritance
- JavaScript - Abstraction
- JavaScript - Polymorphism
- JavaScript - Destructuring Assignment
- JavaScript - Object Destructuring
- JavaScript - Array Destructuring
- JavaScript - Nested Destructuring
- JavaScript - Optional Chaining
- JavaScript - Global Object
- JavaScript - Mixins
- JavaScript - Proxies
- JavaScript Versions
- JavaScript - History
- JavaScript - Versions
- JavaScript - ES5
- JavaScript - ES6
- ECMAScript 2016
- ECMAScript 2017
- ECMAScript 2018
- ECMAScript 2019
- ECMAScript 2020
- ECMAScript 2021
- ECMAScript 2022
- JavaScript Cookies
- JavaScript - Cookies
- JavaScript - Cookie Attributes
- JavaScript - Deleting Cookies
- JavaScript Browser BOM
- JavaScript - Browser Object Model
- JavaScript - Window Object
- JavaScript - Document Object
- JavaScript - Screen Object
- JavaScript - History Object
- JavaScript - Navigator Object
- JavaScript - Location Object
- JavaScript - Console Object
- JavaScript Web APIs
- JavaScript - Web API
- JavaScript - History API
- JavaScript - Storage API
- JavaScript - Forms API
- JavaScript - Worker API
- JavaScript - Fetch API
- JavaScript - Geolocation API
- JavaScript Events
- JavaScript - Events
- JavaScript - DOM Events
- JavaScript - addEventListener()
- JavaScript - Mouse Events
- JavaScript - Keyboard Events
- JavaScript - Form Events
- JavaScript - Window/Document Events
- JavaScript - Event Delegation
- JavaScript - Event Bubbling
- JavaScript - Event Capturing
- JavaScript - Custom Events
- JavaScript Error Handling
- JavaScript - Error Handling
- JavaScript - try...catch
- JavaScript - Debugging
- JavaScript - Custom Errors
- JavaScript - Extending Errors
- JavaScript Important Keywords
- JavaScript - this Keyword
- JavaScript - void Keyword
- JavaScript - new Keyword
- JavaScript - var Keyword
- JavaScript HTML DOM
- JavaScript - HTML DOM
- JavaScript - DOM Methods
- JavaScript - DOM Document
- JavaScript - DOM Elements
- JavaScript - DOM Forms
- JavaScript - Changing HTML
- JavaScript - Changing CSS
- JavaScript - DOM Animation
- JavaScript - DOM Navigation
- JavaScript - DOM Collections
- JavaScript - DOM Node Lists
- JavaScript Miscellaneous
- JavaScript - Ajax
- JavaScript - Async Iteration
- JavaScript - Atomics Objects
- JavaScript - Rest Parameter
- JavaScript - Page Redirect
- JavaScript - Dialog Boxes
- JavaScript - Page Printing
- JavaScript - Validations
- JavaScript - Animation
- JavaScript - Multimedia
- JavaScript - Image Map
- JavaScript - Browsers
- JavaScript - JSON
- JavaScript - Multiline Strings
- JavaScript - Date Formats
- JavaScript - Get Date Methods
- JavaScript - Set Date Methods
- JavaScript - Modules
- JavaScript - Dynamic Imports
- JavaScript - BigInt
- JavaScript - Blob
- JavaScript - Unicode
- JavaScript - Shallow Copy
- JavaScript - Call Stack
- JavaScript - Reference Type
- JavaScript - IndexedDB
- JavaScript - Clickjacking Attack
- JavaScript - Currying
- JavaScript - Graphics
- JavaScript - Canvas
- JavaScript - Debouncing
- JavaScript - Performance
- JavaScript - Style Guide
- JavaScript Useful Resources
- JavaScript - Questions And Answers
- JavaScript - Quick Guide
- JavaScript - Functions
- JavaScript - Resources
JavaScript - Display Objects
Displaying Objects in JavaScript
There are different ways to display objects in JavaScript. Using the console.log() method, we can display the object in the web console. Sometimes developers require to display the object properties and their value in the HTML or for debugging the code.
For displaying an object, we can access the different properties and display them. We can also convert the object to a JSON string and display it as a string.
When you print the object like other variables in the output, it prints the '[object Object]' as shown in the example below.
In the example below, we have created a fruit object and printed it in the output. You can observe that it prints the [object Object].
<html> <body> <p id = "output">The object is: </p> <script> const fruit = { name: "Banana", price: 100, } document.getElementById("output").innerHTML += fruit; </script> </body> </html>
Output
The object is: [object Object]
To overcome the above problem, you need to use specific approaches to display the object.
Some approaches to display the JavaScript objects are as follows −
Accessing the object properties
Using the JSON.stringify() method
Using the Object.entries() method
Using the for...in loop
Accessing the Object Properties
In the object properties chapter, you learned different ways to access the values of the object properties. You can use the dot notation or square bracket notation to display the property values.
This way, you may get all property values and display them in the output.
Syntax
Users can follow the syntax below to display the object by accessing properties.
obj.property; OR obj["property"];
In the above syntax, we access the property using the obj object's dot and square bracket notation.
Example
In the example below, we have accessed the 'name' property of the object using the dot notation and the 'price' property using the square bracket notation.
<html> <body> <p id="output"> </p> <script> const fruit = { name: "Banana", price: 100, } const fruitName = fruit.name; const fruitPrice = fruit["price"]; document.getElementById("output").innerHTML = "The price of the " + fruitName + " is: " + fruitPrice; </script> </body> </html>
Output
The price of the Banana is: 100
Using the JSON.stringify() Method
When object contains the dynamic properties or you don't know object properties, you can't print properties and values using the first approach. So, you need to use the JSON.stringify() method. It converts the object into a string.
Syntax
Follow the syntax below to use the JSON.stringify() method to display the object.
JSON.stringify(obj);
You need to pass the object as a parameter of the JSON.stringify() method.
Example
In the example below, we have converted the fruit string into the JSON string and displayed in the output.
<html> <body> <p id = "output">The fruit object is </p> <script> const fruit = { name: "Banana", price: 100, } document.getElementById("output").innerHTML += JSON.stringify(fruit); </script> </body> </html>
Output
The fruit object is {"name":"Banana","price":100}
Using the Object.enteries() Method
The Object.entries() is a static method of the Object class, allowing you to extract the properties and values in the 2D array. After that, you can use the loop to traverse the array and display each property and value pair individually.
Syntax
Follow the syntax below to use the Object.entries() method.
Object.entries(obj);
The Object.entries() method takes the object as a parameter and returns the 2D array, where each 1D array contains the key-value pair.
Example
In the example below, the numbers object contains the 4 properties. We used the Object.entries() method to get all entries of the object.
After that, we used the for loop to traverse the object entries and display them.
<html> <body> <p> Displaying the object entries</p> <p id = "output"> </p> <script> const numbers = { num1: 10, num2: 20, num3: 30, num4: 40, } for (const [key, value] of Object.entries(numbers)) { document.getElementById("output").innerHTML += key + ": " + value + " <br>"; } </script> </body> </html>
Output
Displaying the object entries num1: 10 num2: 20 num3: 30 num4: 40
Using the for...in Loop
The for...in loop is used to traverse the iterable, and the object is one of them.
Syntax
Users can follow the syntax below to use the for...in loop to traverse the object and display it in the output.
for (key in obj) { // Use the key to access the value }
In the above syntax, obj is an object to display. In the loop body, you can access the value related to the key and print the key-value pair.
Example
In the example below, we used the for...in loop to traverse each key of the object and print them in the output.
<html> <body> <p> Displaying Object Entries using for...in loop:</p> <p id = "output"> </p> <script> const languages = { language1: "JavaScript", language2: "Python", language3: "Java", language4: "HTML", } for (const key in languages) { document.getElementById("output").innerHTML += key + ": " + languages [key] + " <br>"; } </script> </body> </html>
Output
Displaying Object Entries using for...in loop: language1: JavaScript language2: Python language3: Java language4: HTML
The best way to display the object is using the JSON.stringify() method. It converts the object into a flat string. Other approaches can't be used to display the nested objects, but JSON.stringify() method can be used.