Properties are the values associated with a JavaScript object.
A JavaScript object is a collection of unordered properties.
Properties can usually be changed, added, and deleted, but some are read only.
Accessing JavaScript Properties
The syntax for accessing the property of an object is:
objectName.property // person.age
or
objectName["property"] // person["age"]
or
objectName[expression] // x = "age"; person[x]
The expression must evaluate to a property name.
Example 1
person.firstname + " is " + person.age + " years old.";
Example 2
person["firstname"] + " is " + person["age"] + " years old.";
JavaScript for...in Loop
The JavaScript for...in
statement loops through the properties of an object.
Syntax
for (let variable in object) {
// code to be executed
}
// code to be executed
}
The block of code inside of the for...in
loop will be executed once for each property.
Looping through the properties of an object:
Example
const person = {
fname:" John",
lname:" Doe",
age: 25
};
for (let x in person) {
txt += person[x];
}
fname:" John",
lname:" Doe",
age: 25
};
for (let x in person) {
txt += person[x];
}
Practice Excercise Practice now