For Each Loop in Mozilla 14

I’m not entirely sure when Mozilla started to support for each loops but I guess it was part of the E4X code checkins. Anyway, I was totally shocked when I saw it and I fired up a quick test.

var o = {a: "A", b: "B", c: "C"};
for each (var v in o) {

var a = ["A", "B", "C"];
for each (v in a) {

window.onload = function () {
   var sb = [];
   for each (var el in document.getElementsByTagName("*")) {

Thank god for Mozilla

  • ioriwellings

    Wrong in FireFox or Opera.

    by the way, what’s E4X?

  • minghong

    No, it works prefectly if you are using Deer Park Alpha 2 (not sure about Alpha 1, probably works too). E4X stands for ECMAScript for XML.

    “for each” seems to be more suitable instead of “for in”, which scans all the object under the subject, e.g. length in Array.

    Try to rewrite the second for each loop to for in loop and you’ll notice the differences.

  • minghong

    Correction: rewriting the second loop will give the same result. I can’t find the right example yet. :-P

  • Emil A Eklund

    Thats great news. While not the most exciting addition they are quite convenient.

  • nitro2k01

    I don’t get the point…
    Isn’t this exactly what you have been able to do for years using
    for (i in v){}
    Or is there some subtle difference? (Now I see, it returns the actual object, not the index)

    var o = {a: “A”, b: “B”, c: “C”};
    for (v in o) {

    var a = ["A", "B", "C"];
    for (v in a) {

    window.onload = function () {
    var sb = [];
    for (el in document.getElementsByTagName(“*”)) {
    seems to be working fine cross-browser.
    So right, this is convenient, but nowhere near revolutionary…

  • Erik Arvidsson

    It is nothing revolutionary. The good thing is that Mozilla realizes that there is a need to improve JS 1.x.

  • Hakan Bilgin

    Well, in that case…
    - Mozilla can render HTML, thank god for Mozilla. ;-)

  • Erik Arvidsson

    Hakan: I’m not sure what you are trying to say here?

  • Hakan Bilgin

    I was a little bit sarcastic. Even thoguh this ain’t so big a deal, you’re taking every opportunity to exaggerate Firefox. ‘Coz the feature with different version of browsers, and code that works on a few of them, is also available on IE. tihi

    Anyway…hope you aren’t offended. I’m just expressing differing opinions.

  • nitro2k01

    HBI! Great to see that you read Erik’s blog. (I found it through challenger’s home page, so I should’ve guessed)
    You’re post is a bit of freudian mis-saying. If you had added an X to HTML that would be a totally true statement.
    Fact is that IE is a browser which barely has been updated for a few years. (Except for security patches) Alot can be done with the custom DOM of IE, but you can’t escape from the fact that IE lacks proper support for XHTML, and that jscript has quite a few memomry leaks. (Look here: )

  • Hakan Bilgin

    Long time no see, pal…How’s everything? I read Erik’s blog when ever I can, even though I sometimes don’t agree I appreciate and recognize the guru. Now to my response…

    Sometimes I really get confused by the anti-MS propaganda. No to pathes but yes to upgrades? Hmmm…

    Many years ago, I was at a seminar. The lecturer said something I find fitting here:
    “There are two kinds of people. The ones in the box, complaining about how the walls are too limiting. The other ones, that are outside the box, pushing or pulling the box and taking advantage of the box’s shape, size and color.”

    Instead of complaining, Mozart created wonderful music (even though the lack of synths and equalizers) and Da Vinci painted beautiful paintings (even though the lack of Photoshop…haha). Either accept the tools that are at hand and create or don’t.

    Alright, I agree…one should always have the right of complaining but don’t dismiss the fact that one can achieve wonders, independently of the tools that are at hand, right now. Erik, Emil and others has prooven that in the past. But enough with this grumbling phase allready…


  • Erik Arvidsson

    I’m not sure I complain too much. I try identify issues that should not be tolerated in a browser. If you don’t know about the issues it is hard to work around them. Most of the times the issues I describe are with IE but I agree that I haven’t been too good at pointing fingers at Moz, Opera and KHTML. Opera and KHTML/Safari are a lot worse than IE and that is why most applications work in IE and Mozilla. It is just that Opera and KHTML can be ignored in most cases. IE cannot be ignored.

    Another reason why I complain about IE: I want MSFT to fix these issues.

    A third reason is so more people move away from IE. When people started to move away from IE to Firefox MSFT assembled the old IE team and started its development again. And I want IE and the web to improve.

  • alex

    Very interesting, only in mozilla not correct
    view many sites
    example :

  • sucheta

    i want to complete definition and example of for each loop. i m not sure in this example what he trying to show.And difference between for in and for each loop.