Javascript foreach() function

This question already has an answer here:

  • For-each over an array in JavaScript? 23 answers

  • Is very important if you are going to use in to check with hasOwnProperty if not you can get more values specially if you are using libraries like prototype. hasOwnProperty will check if the property is direct property of the object

        for (var i in list) {
            if (list.hasOwnProperty(i)){
                console.log(i);
            }
        }
    

    simply said:

    [1,2,3,4].forEach(function(i) { console.log(i); });
    

    but as أنيس بوهاشم@ suggests, it's better to use:

    for(var i in list) {
        console.log(list[i]);
    }
    

    because you're avoiding the use of a function call.

    But my best advice to you would be to start first by opening a book about Javascript, or some good web course.

    链接地址: http://www.djcxy.com/p/12070.html

    上一篇: JavaScript如何通过数组foreach循环

    下一篇: JavaScript foreach()函数