Skip to content

Latest commit

 

History

History
63 lines (43 loc) · 2.27 KB

object.md

File metadata and controls

63 lines (43 loc) · 2.27 KB

🔓 Object

If I were to create variables called name, age, DOB, address of a person, i can just simply type:

let name = 'Cheong';
let age = 18;
let dob = '01/01/2002';

However, if i were to create the this properties of 10 person and printing it one by one, this will be a lot of work. Hence, we can use object to store the properties and then key in the values.

✔️ Creating an Object

Let's say I want to create an object named person, with properties as stated above. I just need to type:

let person = {
    name: 'Cheong',
    age: 18,
    dob: '01/01/2002'
};

So if i want to take a look of what is stored in this person, i can just console.log(person)in the console. And I will see this:

Output



Upon Expanding




✔️ To print the properties of an object

We can use for in loop to print the properties of the object, in this case, name, age and dob.

for (let properties in person ){
    console.log(properties); 
}

However, if i were to print both the properties and values of the object, i can use the code shown below:

for (let properties in person ){
    console.log(properties,person[properties]);
}

⚠️ person.propertiesshows the value of the properties.






◀️ Previous Page : Loops : For Loop 🔑                                             🏡                                             ▶️ Next Page : Control Flow : Array 🚩 🔓