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
Search This Blog
Blog Archive
-
▼
2009
(257)
-
▼
June
(31)
- Google Voice gives One number for all your phones
- CellPhones will act as Credit Card by next Year.
- Get Tweet to know when ISS (International Space St...
- Google announces Dashboard for providing detailed ...
- Google focuses more on Mobile devices (iPhone and ...
- Take care of letter case when specifying colSpan a...
- How to force FireFox to open new window instead of...
- Some useful php functions
- Essential Steps for Software Development
- How to get last row from database table?
- javascript code for preventing data loss in web form.
- How to count number of times the user clicks on a...
- Removing white spaces entered in TextArea tag of H...
- Workaround for inconsistent issue with mySql query...
- Need for having Eval function in javascript
- PHP code for listing files in a Folder
- Reverse Auction and finding best price among multi...
- Comments/Suggestions from QA Expert to improve Quiz
- Google is experimenting Squared which will return ...
- Twitter - Are you hearing this word first time?
- Google is taking steps for preventing Plagiarism i...
- Resolved IE specific issue related to calling func...
- foreach loop equivalent in javascript.
- Is it possible to use same domain name for two ser...
- Use Gravatar to display your icon image when addin...
- Use of tinyurl in Twitter
- Introduction to Ajax and sample code for learning ...
- Advantages of php over asp and converting asp site...
- Bing - Mircrosoft's Decision Engine. Will it perfo...
- Free online Quiz for learning php, javascript, mys...
- PHP function for inserting or updating row into da...
-
▼
June
(31)

AI Course | Bundle Offer | Unlocking AI | Dream Big | Listen to Dream Big
Today's Deals | Timesheet | Products | SQL ebook | 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