To display a JavaScript object, you can use several methods, depending on where you want to show it. Here are the most common ways:
1. Console Output:
Use console.log() to print the object in the browser’s console:
const person = { name: “John”, age: 30 };
console.log(person);
2. Alert Box:
Use alert() to show the object as a string (converted by JSON.stringify()):
const person = { name: “John”, age: 30 };
alert(JSON.stringify(person));
3. Displaying in HTML:
You can display the object on a webpage by converting it into a string and placing it inside an HTML element:
const person = { name: “John”, age: 30 };
document.getElementById(“output”).innerText = JSON.stringify(person);
This will display the object in an HTML element with the id=”output”.