How to loop thru an array

using plain javascript using new Arrya.prototype.forEach underscore jQuery

by ocorpening

HTML

<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js'></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js'></script>

JavaScript

var arr=[1,2,3];
for(var i=0; i<arr.length; i++) // very very oldschool - still the golden method if order must be assured
{
    console.log("i, arr[i] = " + i + ", " + arr[i]);
}
for(var index in arr) // Note: order is not guranteed!!! Skips holes in the Array!!!
{
    console.log("index, item = " + index + ", " + arr[index]);
}
arr.forEach(function(element) // ES5
{
    console.log("element = " + element);
});
_.each(arr, function(element, index, list) // underscore
{
    console.log("index, element = " + index + ", " + element);
});
$.each(arr, function(index, val) // jQuery
{
    console.log("index, val = " + index + ", " + val);
});