Answers for "for of vs for in javascript"

10

for of vs for in javascript

let list = [4, 5, 6];

for (let i in list) {
   console.log(i); // "0", "1", "2",
}

for (let i of list) {
   console.log(i); // "4", "5", "6"
}

// EXAMPLE 2
let pets = new Set(["Cat", "Dog", "Hamster"]);
pets["species"] = "mammals";

for (let pet in pets) {
   console.log(pet); // "species"
}

for (let pet of pets) {
    console.log(pet); // "Cat", "Dog", "Hamster"
}
Posted by: Guest on October-11-2020
1

Flutter For In loop explained

void main() {
  List subjects = ['Math', 'Biology', 'Economic', 'History', 'Science'];
  for (String sName in subjects) {
    print(sName);
  }
}
Posted by: Guest on April-09-2020
0

for in and for of in js

Object.prototype.objCustom = function () {}; 
Array.prototype.arrCustom = function () {};

let iterable = [3, 5, 7];
iterable.foo = "hello";

for (let i in iterable) {
  console.log(i); // logs 0, 1, 2, "foo", "arrCustom", "objCustom"
}

for (let i of iterable) {
  console.log(i); // logs 3, 5, 7
}
Posted by: Guest on September-29-2020
-2

for of vs for in

//for in, iterates keys in an object
var obj={a:1, b:2}

for( var key in obj)
  console.log(obj[key]); //would print 1 and 2

//for of, iterates values in array or any iterable
var ar = [10, 11, 12, 13]
for (var item in ar )
  console.log(item);  //would print 10  11  12  13
Posted by: Guest on August-13-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language