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.
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:
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.properties
shows the value of the properties.