Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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"
}
Comment

javascript for loops in vs of

let myArray = ["a", "b", "c"];

// for...in iterates over properties (like index)
for (let i in myArray){
  console.log(i); // → 0, 1, 2
} 

// for...of iterates over values
for (let i of myArray){
  console.log(i); // → a, b, c
} 
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to read a json file in node js 
Javascript :: javascript remove first space in string 
Javascript :: how to show only time in hours and minutes only in javascript 
Javascript :: datatable after loading function 
Javascript :: datepicker get selected date 
Javascript :: docker react module not found 
Javascript :: js when key is pressed 
Javascript :: remove all spaces from string javascript 
Javascript :: pick random from array 
Javascript :: react get current date yyyy-mm-dd 
Javascript :: onclick event in angular 
Javascript :: local storage angular 
Javascript :: next js fallback 
Javascript :: html add new line in js alert 
Javascript :: js when you leave 
Javascript :: javascript object first key 
Javascript :: how to change css with js 
Javascript :: how to get last element of array 
Javascript :: how to get xhr response in javascript 
Javascript :: javascript last element in array 
Javascript :: javascript last index 
Javascript :: how to swap two variables in js 
Javascript :: electron Uncaught ReferenceError: require is not defined at recorder.js:1 
Javascript :: get current data and time in javascript 
Javascript :: array from comma separated string javascript 
Javascript :: jquery get unique values from array 
Javascript :: moment js year only 
Javascript :: react native width auto 
Javascript :: get all classes of element jquery 
Javascript :: javascript get date name 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =