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...
Shop at Rajamanickam.com | Birthday Gift Idea? | Hire me for $6 per Hour
Get 3 useful ebooks for Rs 99 in India and $5.99 globally
Get a 75% commission | ChatGPT and and Google Gemini for Beginners (Use Discount code QPT)
Monday, June 8, 2009
Search This Blog
Art of Talking to AI | Tech eBook | Dream Big | Listen to Dream Big
Today's Deals | Timesheet | Products | 3 ebooks for $5.99 / Rs 99 | Earn 50% commission
About | Privacy | Follow | TOS | WhatsApp | Contact
I may earn a commission from Amazon affiliate links
Today's Deals | Timesheet | Products | 3 ebooks for $5.99 / Rs 99 | Earn 50% commission
About | Privacy | Follow | TOS | WhatsApp | Contact
I may earn a commission from Amazon affiliate links
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