Objects

Creating and Accessing

let person={
    name:'Vignesh',
    age:19,
    roll:"22501A05J1"
}
console.log(person);

console.log(person.name);
console.log(person.age);
console.log(person.roll);

Iteration in Objects

let person={
    name:'Vignesh',
    age:19,
    roll:"22501A05J1"
}
console.log(person);

// for in loop
for(let key in person){
    console.log(person[key]);
}

Operations in Objects - Insert, Delete, Update

let person={
    name:'Vignesh',
    age:19,
    roll:"22501A05J1"
}
console.log(person);

// Insert
person.city='Vijayawada';
console.log(person);

// Update
person.city='Hyderabad';
console.log(person);

// Delete
delete person.city;
console.log(person);

Complex Object

let person={
    name:'Vignesh',
    age:19,
    roll:"22501A05J1",
    marks:[10,20,30,40,50],
    address:{
        city:'Vijayawada',
        state:'Andhra Pradesh',
        country:'India'
    },
    getAverage:function(){
        let sum=0;
        for(let mark of this.marks){
            sum+=mark;
        }
        return sum/this.marks.length;
    }
}
console.log(person);
console.log(person.marks);
console.log(person.getAverage());

Methods

Calling methods of a object is done by using object Reference. *objectName.methodName();*

let student={
    name:'Pavan',
    age:18,
    city:'VJA',
    marks:{
        maths:90,
        science:80,
        english:85
    },
    getAvg:function(){
        return (this.marks.maths+this.marks.science+this.marks.english)/3;
    }
};
console.log(student.getAvg());

This Keyword: