- for v in obj/array , works on both obj(like a map) and array:
for obj:
var user = {
first_name: "CSS",
last_name: "HTML",
age: 4,
/*from www.w3cschool.cn*/
website: "www.w3cschool.cn"
};
> for(v in user) console.log(v);
first_name
last_name
age
website
> for(v in user) console.log(user[v]);
CSS
HTML
4
www.w3cschool.cn
//obj does not allow `for of` statement
> for(v of user) console.log(v);
TypeError: user is not iterable
for array:
> for(v of arr) console.log(v);
10
9
8
> arr=[10,9,8]
[ 10, 9, 8 ]
> for(v in arr) console.log(arr[v]);
10
9
8
//`for of` works in array since array can be iterated
> for(v of arr) console.log(v);
10
9
8