1let list = [4, 5, 6];
2
3for (let i in list) {
4 console.log(i); // "0", "1", "2",
5}
6
7for (let i of list) {
8 console.log(i); // "4", "5", "6"
9}
10
11// EXAMPLE 2
12let pets = new Set(["Cat", "Dog", "Hamster"]);
13pets["species"] = "mammals";
14
15for (let pet in pets) {
16 console.log(pet); // "species"
17}
18
19for (let pet of pets) {
20 console.log(pet); // "Cat", "Dog", "Hamster"
21}
1void main() {
2 List subjects = ['Math', 'Biology', 'Economic', 'History', 'Science'];
3 for (String sName in subjects) {
4 print(sName);
5 }
6}
7
1Object.prototype.objCustom = function () {};
2Array.prototype.arrCustom = function () {};
3
4let iterable = [3, 5, 7];
5iterable.foo = "hello";
6
7for (let i in iterable) {
8 console.log(i); // logs 0, 1, 2, "foo", "arrCustom", "objCustom"
9}
10
11for (let i of iterable) {
12 console.log(i); // logs 3, 5, 7
13}
14
1//for in, iterates keys in an object
2var obj={a:1, b:2}
3
4for( var key in obj)
5 console.log(obj[key]); //would print 1 and 2
6
7//for of, iterates values in array or any iterable
8var ar = [10, 11, 12, 13]
9for (var item in ar )
10 console.log(item); //would print 10 11 12 13