Object Literals

const person = {
    firstName: 'Gabriel',
    lastName: 'Flames',
    age: 37,
    email: 'info@wpflames.com',
    hobbies: ['karate', 'ju jitsu', 'aikido'],
    address: {
        city: 'Miami',
        state: 'FL'
    },
    getBirthYear: function(){
        return 2021 - this.age;
    }
}

let val;

val = person;

// Get specific value
val = person.firstName;
val = person['firstName'];
val = person.age;
val = person.hobbies[1];
val = person.address['city'];
val = person.getBirthYear();

console.log(val);

const people = [
    {name: 'John', age: 20},
    {name: 'Jack', age: 22},
    {name: 'Julie', age: 18}
];

for(let i = 0; i < people.length; i++){
    console.log(people[i].name);
}
Was this page helpful?

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *