with Imagination: by Dustin Diaz

./with Imagination

A JavaScript, CSS, XHTML web log focusing on usability and accessibility by Dustin Diaz

for each in JavaScript

Friday, May 25th, 2007

I felt that this deserves a tad more attention than just adding this to my del.icio.us bookmarks, but just moments ago I stumbled onto a JavaScript reference page on Mozilla’s developer center that spoke of this intrigue about for each ( … in … ). I had no idea this existed! I looked over at Dan Webb who’s sitting right across from me, and he agreed, it’s news to him too. Granted it was implemented in JavaScript 1.6 (which means it will work in Firefox 1.5), but as far as I knew, these were the only things new in JavaScript 1.6.

Anyhow, it’s nothing that special. It simply allows you to iterate over the values of an object, and not the keys. Nevertheless, it’s nothing you can’t already do with just a regular for (in) which returns you the keys of an object… so if you have the keys, and the object… you indeed, have access to the values. Go ahead, have a look.

16 Responses to “for each in JavaScript”

  1. David Joseph

    I’m confused unless it’s just thats it’s been updated since you posted the second link you gave “<a href="http://developer.mozilla.org/en/docs/New_in_JavaScript_1.6">this link</a>” has foreach there 3rd one down in “iterative methods”… Have I missed something?

  2. Dustin Diaz

    David, note the difference between forEach, and for each (in). The former is an Array method, and the latter is a statement. Take a peak at the 1.5 reference guide.

  3. David Joseph

    Ah Cool I thought I must have misinterpereted, my bad.

  4. Asanka

    Since I found jQuery, JavaScript is not the same to me, rather than programming with JavaScript I tend to program on top of jQuery.

    $.each() - for arrays and objects

    Libraries take over the language, sometimes takes over other libraries, for example I find your library easier to digest than the YUI library it self. It is the era of libraries and frameworks I guess.

  5. Dustin Diaz

    For the record, $.each or Array.prototype.forEach are not the same as for each (...in...). Please read this post carefully.

  6. Radoslav Stankov

    The new things in Javascript are only added to FireFox, but Opera and especially IE don’t have them(unfortunately) :( .

  7. Nicholas C. Zakas

    The confusion is because for each is part of the ECMAScript for XML specification, which was implemented along with JavaScript 1.6. Check out the spec (http://www.ecma-international.org/publications/standards/Ecma-357.htm) and go to page 63, section 12.3. Of course, just because for each is part of E4X doesn’t mean that it can’t be used in normal JavaScript as well. It’s a little confusingly named but otherwise a nice addition to the language.

  8. Asanka

    Sorry for the misunderstanding. I wasn’t saying this is the same as $.each. Just that, due to other available methods to get things done, actual capabilities/functions of are overlooked by guys like me. So, thanks for pointing out the right stuff.

  9. Carson

    Holy crap! Where has this been hiding? Awesome. I’m using this today. Great find, thanks D.

  10. Alice

    Great find Dustin, quick question does Mozzila plan on making translations available in other languages other than Fench anytime this year?

  11. second life 4 eva

    same here. iliketitallyloveit. well, admittedly after running into the above mentioned “forEach” and “for each” trap…

    i know, girls and coding. ;) but now its working fine#

    cheers,
    eva

  12. Bhasker V Kode

    Hey Dustin,
    There’s an equally nifty one which is sort of an alliteration of the for loop,but takes a similiar style as the for each you mentioned above. Have a look !

    // general usage
    for (var obj1 in obj2){
     …
    }
    
    //example 1
    for (var obj in window){
     document.write( ‘window.’+obj +’ =>’+ window[obj] );
    }
    
    //example 2
    var a={name:’asd’,id:123} , b;
    for(var obj in a){
     b = typeof a[obj];
     console.log( b +’=>’+obj+’=>’+ a[obj] );
    }
    

    Keep Clicking,
    Bhasker V Kode,
    returnable.org

    PS: i think that you can seriously contemplate adding a ‘preview comments’ feature!

  13. Pimpster

    Greaet find indeed :)

  14. Web design bulgaria

    Its very reasy when you use JQUERY (search google)

    its like this:

    
    $.each( { name: "John", lang: "JS" }, function(i, n){
      alert( "Name: " + i + ", Value: " + n );
    });
    
  15. MikeFM

    Sadly, I found it does not work in IE. Darn IE.

  16. MPS

    Am I the only one who finds it annoying that Javascript still requires a traditional “for” loop to iterate an Array?

    Why include a “for…in” construct that doesn’t let you iterate an Array?

    Even prototype “.each” doesn’t yield good performance. Sad.

Leave a Reply

Phone Number:

If you're about to post code in your comment, please wrap your code with the tag-combo <pre><code>. Also please escape your html entities - otherwise they will be stripped out. I recommend using postable.

Get "JavaScript Design Patterns"

"As a web developer, you'll already know that JavaScript™ is a powerful language, allowing you to add an impressive array of dynamic functionality to otherwise static web sites. But there is more power waiting to be unlocked--JavaScript is capable of full object-oriented capabilities, and by applying OOP principles, best practices, and design patterns to your code, you can make it more powerful, more efficient, and easier to work with alone or as part of a team."

Buy JS Design Patterns from Amazon.com Buy JS Design Patterns from Apress

Flickr

Submit a Prototype

All content copyright © 2003 - 2007 under the Creative Commons License. Wanna know something? Just ask.

About | Archives | Blog Search

[x] close

Loading...

Submit a prototype

By checking this prototype I agree that I am not submitting false credentials, pornography, or a hate crime website. I also understand that by submitting my entry I may or may not be accepted, and if accepted, my entry may be taken down at any given time if I violate these terms.