Something Like that:
Object and Object literals
What is Object and Object literals
Both are work same.
Object
For that you need to create first html code.
HTML
<p>Value of a = <span id="demo"></span></p>
After that add this JS code.
JS
var person = new Object(); person.firstname = "Tony"; person.lastname = "Alica"; person.address = new Object(); person.address.street = '111 Main' document.getElementById("demo").innerHTML = person; console.log(person);
[codepen_embed height=”265″ theme_id=”0″ slug_hash=”ZJBJWN” default_tab=”result” user=”pradeepanvi”]See the Pen Object in javascript by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]
Now you can see
Value of a = [object Object]
Looks strange?
In the code you can see one more line.
JS
console.log(person);
How can I see console?
Just Ctrl+Shift+I or F12
Click on Console tab
Now Object literals
JS
var person = { firstname: 'Tony', lastname: 'Alica', address: { street: '111 Main' } }; document.getElementById("demo").innerHTML = person; console.log(person);
[codepen_embed height=”265″ theme_id=”0″ slug_hash=”zdodzG” default_tab=”js” user=”pradeepanvi”]See the Pen Object literals in javascript by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]
Now you can see
Value of a = [object Object]
and check console window.
Both result are same but point is best way to write code from my side Object literals is best.