An object is a collection of key-value(properties and values) pairs.
In other terms they are variables that can store both values and functions.
Functions are stored as key:function() pairs called methods.
constperson={name:"Varun",age:6,isStudent:true};document.write(person.name+"<br>");document.write(person.age+"<br>");document.write(person.isStudent+"<br>");Output:Varun6true//Here name,age and isStudent are keys. Varun,6 and true are values.
Accessing object keys using Bracket Notation(useful for dynamic keys) :
constperson={name:"Varun",age:6,isStudent:true};person.city="chennai";//adding new propertyperson.age=7//updating propertydocument.write(person.name+"<br>");document.write(person.age+"<br>");document.write(person.isStudent+"<br>");document.write(person.city+"<br>");Output:Varun7truechennai
constperson={name:"Varun",age:6,greet:function(){return"Hi, I am "+this.name+".";//'this' refers to this object},play:()=>{return"I am Playing."}};document.write(person.greet()+"<br>");document.write(person.play());Output:Hi,IamVarun.IamPlaying.
Nested objects :
A Nested Object is simply an object inside another object.
Top comments (0)