JavaScript Array forEach examples
By:Roy.LiuLast updated:2019-08-11
In JavaScript, we can use forEach(function) to loop an Array. The provided function in forEach() will run once for each array element.
var total = 0; var num = [1, 2, 3, 4, 5]; num.forEach(anyFunction) function anyFunction(data){ total += data; console.log(total); //15 var words = ""; var num = ["a", "b", "c", "d", "e"]; num.forEach(anyFunction2) function anyFunction2(data, index){ words += data; console.log(index + ":" + data); //0:a, 1:b, 2:c, 3:d, 4:e console.log(words); //abcde
From:一号门
Previous:JavaScript How to loop an Array
Next:log4j2.xml example
COMMENTS