JavaScript How to loop an Array
By:Roy.LiuLast updated:2019-08-11
In JavaScript, we can use for, forEach or for..of to loop an Array.
1.1 for
//old-school, classic for loop var str = ["a", "b", "c", "d", "e"]; for(var i =0; i < str.length; i++){ console.log(str[i]); //a,b,c,d,e
output
1.2 forEach()
var num = [1, 2, 3, 4, 5]; num.forEach(anyFunction) function anyFunction(data){ console.log(data);
output
1.3 for..of
var strArray = ["a", "b", "c", "d", "e"]; for(var s of strArray){ console.log(s);
output
From:一号门
Previous:log4j2.properties example
COMMENTS