About Lesson
JavaScript Display Objects
Displaying objects in JavaScript can be done using various methods, such as console.log()
, alert()
, and document.write()
.
Using console.log()
The console.log()
method is commonly used to display objects in the browser console for debugging purposes.
Example:
const person = {
firstName: "John",
lastName: "Doe",
age: 30
};
console.log(person);
Using alert()
The alert()
method displays a dialog box with a specified message.
Example:
alert(JSON.stringify(person));
Using document.write()
The document.write()
method writes HTML expressions or JavaScript code to a document.
Example:
document.write(JSON.stringify(person));
When displaying objects in JavaScript, it’s essential to choose the appropriate method based on your use case, whether it’s for debugging, user interaction, or dynamically updating HTML content.