foreach loop will be easy to use when there is a need to iterate thro' an array or any other collection.
But javascript is not having this option.
Anyway, we can use below type of for loop for achieving the same result of foreach loop.
var arrItems = ["item1","item2","item3"];
for ( var i in arrItems )
{
alert( arrItems[i] );
}
The above code will alert each item in the array one by one.
More Articles...
Monday, June 8, 2009
Popular Posts
- Software Testing Quiz Questions and Answers
- Javascript Quiz Questions and Answers
- MySQL Quiz Questions and Answers
- SEO Your Blog the Easy Way - Guest Post
- SEO (Search Engine Optimization) Quiz Questions and Answers.
- HTML Quiz Questions and Answers
- PHP Quiz Questions and Answers
- Best Motivational Quotes Book | Kindle eBook and Paperback | Read for FREE with Amazon's Kindle Unlimited
- Basic Computer Hardware Quiz Questions and Answer.
- Use of tinyurl in Twitter

3 comments:
Nice to know! Peace
This code will spit out all properties of an object. It is useful to use.hasOwnProperty(i) when dealing with objects that inherit from a parent. Trust me, this one line check will save hours of debugging.
Dude foreach in javascript is bad should avoid it. Array in javascript is not like other languages, looping the way you have mentioned will spit the all method of the Array. I would recommend people to use like this
foo = [1,32,12,43]
for(var i=0;i<foo.length;i++){
foo[i];
}
Post a Comment