const people =[{name:'Karl',location:'UK'},{name:'Steve',location:'US'}];for(const person of people){console.log(person.name);// "karl", then "steve"console.log(person.location);// "UK", then "US"}
let colors =['Red','Green','Blue'];for(const[index, color]of colors.entries()){console.log(`${color} is at index ${index}`);}Code language:JavaScript(javascript)
let list =[10,11,12];for(let i in list){console.log(i);//Display the indices: "0", "1", "2",}for(let i of list){console.log(i);// Display the elements: "10", "11", "12"}
const array =['a','b','c','d'];for(const item of array){console.log(item)}// Result: a, b, c, dconst string ='Ire Aderinokun';for(const character of string){console.log(character)}// Result: I, r, e, , A, d, e, r, i, n, o, k, u, n
let arr =['el1','el2','el3'];
arr.addedProp='arrProp';// elKey are the property keysfor(let elKey in arr){console.log(elKey);}// elValue are the property valuesfor(let elValue of arr){console.log(elValue)}
const letters =["a","b","c","d","e","f"];for(const x of letters){console.log(x)}/*note that for of only works for iterable objects(which includes an array). The above type of code would NOT work for a JSON for example*//*for example for something like
const x = {0:0, 1:2222, 2:44444}, you can only use for ... in as JSON is not an iterable*/
//for of for the array values and in for properties of obj and array indexesconst person =['a','b','c'];for(let x in person){document.write([x]);}const person =['a','b','c'];for(let x in person){document.write(person[x]);}for(let x of person){document.write([x]);}
let list =["a","b","c"];// for infor(let i in list){// i is indexconsole.log(i);// "0", "1", "2",console.log(list[i]);// "a", "b", "c"}// for offor(let i of list){// i is valueconsole.log(i);// "a", "b", "c"}