1var colors=["red","blue","green"];
2for (let i = 0; i < colors.length; i++) {
3 console.log(colors[i]);
4}
1const object = {a: 1, b: 2, c: 3};
2
3for (const property in object) {
4 console.log(`${property}: ${object[property]}`);
5}
1var person = {"name":"taylor","age":31};
2for (property in person) {
3 console.log(property,person[property]);
4}
5//name taylor
6//age 31
1Example:
2const object = { a: 1, b: 2, c: 3 };
3
4for (const property in object) {
5 console.log(`${property}: ${object[property]}`);
6}
7
8// expected output:
9// "a: 1"
10// "b: 2"
11// "c: 3"
12
13
14Syntax:
15for (variable in object)
16 statement