You have several options.
If you are using jQuery, you can do this:
$.each(['dog','cat','cow'], function() { var name = this; alert(this); });
If you are not using jQuery, you can create your own function
function each(array, pFunction) { for(var i = 0; i < array.length; i++) { var element = array[i]; pFunction(element); } } each(['dog','cat','cow'], function(name) { alert(name); });
If you don't want to create a function, you can always do something crazy like this: (not recommended)
for (var name in { 'dog' : '','cat' : null,'cow' : null }) { alert(name); }
Luis perez
source share