Learn
Objects
Looping Through Objects (for…in)
Looping through objects can be extremely useful when you want to perform a specific operation on each property within an object. The for...in
loop provides a convenient way to achieve this.
Syntax
The syntax for the for...in
loop is as follows:
Iterating through Object Properties
By using the for...in
loop, we can iterate over each property in an object and perform actions or access their values.
Let's consider an example where we have an object called person
:
We can loop through the properties of this object using the for...in
loop:
The above code will output:
Instructions
1.
Use a for...in
loop to iterate through the person
object:
- Inside the loop, assign each key to the
key
variable. - Use
console.log
to print the value of each key in the following format:<key>: <value>
.
EXPECTED OUTPUT:
Sign up to start coding
Already have an account?
Sign In
Course content