This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Smartphone icons created by Freepik - Flaticon
10.4 Object Display
Displaying a JavaScript object will output [object Object].
var person = {name:"John", age:30, city:"New York"};
document.getElementById("demo").innerHTML = person;
Some common solutions to display JavaScript objects are:
- Displaying the Object Properties by name
- Displaying the Object Properties in a Loop
- Displaying the Object using Object.values()
- Displaying the Object using JSON.stringify()
Displaying Object Properties
The properties of an object can be displayed as a string:
var person = {name:"John", age:30, city:"New York"};
document.getElementById("demo").innerHTML =
person.name + "," + person.age + "," + person.city;
Displaying the Object in a Loop
The properties of an object can be collected in a loop:
var x, txt = "";
var person = {name:"John", age:30, city:"New York"};
for (x in person) {
txt += person[x] + " ";
};
document.getElementById("demo").innerHTML = txt;
- You must use person[x] in the loop.
- person.x will not work (Because x is a variable).
Using JSON.stringify()
Any JavaScript object can be stringified (converted to a string) with the JavaScript function JSON.stringify():
var person = {name:"John", age:30, city:"New York"};
var myString = JSON.stringify(person);
myString is now a JavaScript string, ready to be displayed:
var person = {name:"John", age:30, city: "New York"};
var myString = JSON.stringify(person);
document.getElementById("demo").innerHTML = myString;
- The result will be a string following the JSON notation:
- {"name":"John","age":50,"city":"New York"}
JSON.stringify() is included in JavaScript and supported in all major browsers.
Stringify Dates
JSON.stringify converts dates into strings:
var person = {name:"John", today:new Date()};
var myString = JSON.stringify(person);
document.getElementById("demo").innerHTML = myString;
Stringify Functions
JSON.stringify will not stringify functions:
var person = {name:"John", age:function () {return 30;}};
var myString = JSON.stringify(person);
document.getElementById("demo").innerHTML = myString;
This can be "fixed" if you convert the functions into strings before stringifying.
var person = {name:"John", age:function () {return 30;}};
person.age = person.age.toString();
var myString = JSON.stringify(person);
document.getElementById("demo").innerHTML = myString;
Stringify Arrays
It is also possible to stringify JavaScript arrays:
var arr = ["John", "Peter", "Sally", "Jane"];
var myString = JSON.stringify(arr);
document.getElementById("demo").innerHTML = myString;
- The result will be a string following the JSON notation:
- ["John","Peter","Sally","Jane"]
JavaScript icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon
Example files created in this module:
JS Variables - Objects are Variables part 1
JS Variables - Objects are Variables part 2
JS Objects - Using an Object Literal part 1
JS Objects - Using an Object Literal part 2
JS Objects - Using an Object Literal part 3
JS Objects - Using the Keyword new
JS Objects are Mutable
JS Object Properties - Access a property with .property
JS Object Properties - Access a property with ["property"]
JS Object Properties - Looping object property values
JS Object Properties - Add a new property to an existing object
JS Object Properties - Deleting object properties part 1
JS Object Properties - Deleting object properties part 2
JS Objects - Access nested objects part 1
JS Objects - Access nested objects part2
JS Objects - Access nested objects part 3
JS Objects - Access nested objects part 4
JS Arrays - Nested JS Objects and Arrays
JS Object Methods - this Keyword
JS Objects - Creating and using an object method part 1
JS Objects - Creating and using an object method part 2
JS Objects - Adding a Method to an Object
JS Objects - Using Built-In Methods
JS Objects - Displaying a JavaScript object
JS Objects - Display object properties
JS Objects - Displaying the Object in a Loop
JS Objects - Using Object.values()
JS Objects - Using JSON.stringify()
JS Objects - Stringify Dates
JS Objects - Stringify Functions part 1
JS Objects - Stringify Functions part 2
JS Arrays - Stringify Arrays
JS Getters and Setters - The get Keyword
JS Getters and Setters - The set Keyword
JS Object Methods part 1
JS Object Methods part 2
JS Getters and Setters - Data Quality part 1
JS Getters and Setters - Data Quality part 2
JS Getters and Setters - Object.defineProperty()
JS Object Constructors
JS Object Constructors - Add a Property to an Object
JS Object Constructors - Adding a Method to an Object
JS Object Constructors - Adding a Property to a Constructor part 1
JS Object Constructors - Adding a Property to a Constructor part 2
JS Object Constructors - Adding a Property to a Constructor part 3
JS Object Constructors - Adding a Method to a Constructor
Did You Know?
JS Objects - Using an object constructor
JS Objects - Using the prototype Property part 1
JS Objects - Using the prototype Property part 2
JS Iterables - Iterate over a String
JS Iterables - Iterating Over an Array
JS Iterables - Home Made Iterable part 1
JS Iterables - Home Made Iterable part 2
JS Sets - The new Set() Method part 1
JS Sets - The new Set() Method part 2
JS Sets - The new Set() Method part 3
JS Sets - The add() Method part 1
JS Sets - The add() Method part 2
JS Sets - The forEach() Method
JS Sets - The values() Method part 1
JS Sets - The values() Method part 2
JS Sets - The keys() Method
JS Sets - The entries() Method
JS Map Objects - new Map()
JS Map Objects - Map.set() part 1
JS Map Objects - Map.set() part 2
JS Map Objects - Map.get()
JS Map Objects - Map.size
JS Map Objects - Map.delete()
JS Map Objects - Map.clear()
JS Maps using Map.has()
JS Map Objects - Map typeof
JS Map Objects - Map instanceof
JS Map Objects - Using Map.forEach()
JS Map Objects - Using Map.entries()
JS Map Objects - Using Map.keys()
JS Map Objects - Using Map.values() part 1
JS Map Objects - Using Map.values() part 2
JS Map Objects - Objects as Keys part 1
JS Map Objects - Objects as Keys part 2
JS Object - Changing a Property Value
JS Objects - Listing All Properties
JS Objects - Listing Enumerable Properties
JS Objects - Adding a Property
JS Objects - Adding Getters and Setters
JS Objects - A Counter Example