1let obj = {
2 key1: "value1",
3 key2: "value2",
4 key3: "value3"
5}
6
7Object.keys(obj).forEach(key => {
8 console.log(key, obj[key]);
9});
10// key1 value1
11// key2 value2
12// key3 value3
13
14// using for in - same output as above
15for (let key in obj) {
16 let value = obj[key];
17 console.log(key, value);
18}
1for (var property in object) {
2 if (object.hasOwnProperty(property)) {
3 // Do things here
4 }
5}
6
1let str = "";
2
3for (let i = 0; i < 9; i++) {
4 str = str + i;
5}
6
7console.log(str);
8// expected output: "012345678"
9
1//for ... of statement
2
3const array1 = ['a', 'b', 'c'];
4
5for (const element of array1) {
6 console.log(element);
7}
8
9// expected output: "a"
10// expected output: "b"
11// expected output: "c"
12
1let iterable = [10, 20, 30];
2
3for (let value of iterable) {
4 value += 1;
5 console.log(value);
6}
7// 11
8// 21
9// 31
10
1const array1 = ['a', 'b', 'c'];
2
3for (const element of array1) {
4 console.log(element);
5}