10

I have seen some examples that show that Firefox supports some kind of JavaScript syntax along the lines of *something* if *expression*;.

As an example of what I'm talking about, see this MDN article, which contains the following example:

var evens = [i for each (i in range(0, 21)) if (i % 2 == 0)];

My questions are:

What name would be given to describe this kind of syntax? I mainly want to know this so that I can Google it and read more about it. I have tried Googling the best I can come up with, but haven't been able to put the right terms together to get helpful results.

Can this syntax exist in other places outside of an array comprehension? I feel like I have seen other examples of this being used outside of an array (such as in the example above), but I am not sure.

Where can I read more about this syntax?

Do any other browsers support this besides Firefox?

Is this feature in ES5 or planned for ES-harmony?

nicael
  • 18,550
  • 13
  • 57
  • 90
Nathan Wall
  • 10,530
  • 4
  • 24
  • 47
  • _"What name would be given to describe this kind of syntax?"_ - The article you linked to calls it "Array Comprehensions". – nnnnnn Sep 23 '12 at 23:34
  • I may be wrong, but I think that term was referring to the `for each` inside an array part. I think I have seen this different `if` syntax used outside of arrays, but I am not sure. ? – Nathan Wall Sep 23 '12 at 23:35
  • _Not only is the array comprehension much more compact, but it's actually easier to read, **once you're familiar with the concept**._ I'd only dare using that once I'm certain everyone knows the syntax. The maintainer being a violent psychopath and all... – Laoujin Sep 23 '12 at 23:36
  • _"I think I have seen this different if syntax used outside of arrays"_ - In this case the `if` is still part of the array comprehension. Normally an `if` would have either a statement or a `{ }` block after its closing `)` - is the fact that it doesn't have one in the array comprehension what you mean by "different if syntax"? – nnnnnn Sep 23 '12 at 23:38
  • Related: http://stackoverflow.com/q/1330498/825789 – bfavaretto Sep 23 '12 at 23:54

2 Answers2

4

As others have noted, this is called "Array Comprehensions" and it's one of the many, many features suggested for ECMAScript Harmony:

http://wiki.ecmascript.org/doku.php?id=harmony:array_comprehensions

However, as with pretty much every Harmony "feature", I don't think there's any real notion as to whether it's going to actually be included in the final release. You can use it in Firefox as part of "JavaScript 1.7" (a nebulous "standard" specification that really only applies to Mozilla-based stuff); however, you're better off avoiding FF-specific syntax, exspecially when it would cause syntax errors in other browsers.

You can read more about it by doing a Google search for "Array Comprehensions," but as I mentioned, it's not a very useful tool due to it's Mozilla-specific nature.

You can achieve a similar effect without much more code using the reduce() Array method introduced in ES5:

//JavaScript has no "range" function, so let's make one
var range = function (begin, length) { 
    var i, ret = [];
    for (i = begin; i < begin + length; i++) {
        ret.push(i);
    }
    return ret;
};

var evens = range(0, 21).reduce(function (arr, cur) { 
    if (cur % 2 === 0) arr.push(cur);
    return arr; 
}, []);

That might be a little verbose compared to what you were looking for (even keeping in mind that we had to create a range() function). But it's a relatively compact solution that doesn't require a lot of "setup" and sticks mainly to the solving of the problem: filtering elements from one array to form a second array.

I was able to reduce it to a one-liner, but it becomes a bit unweildy to maintain, so I decided to suggest the two line version instead. In case you're interested in the one-liner, here it is:

//Don't forget to define "range()"
var evens = range(0, 21).reduce(function (arr, cur) { 
    return (cur % 2 === 0) ? (arr.push(cur) && arr) : arr; 
}, []);

Again, this is ES5 code. If you want it to work in older browsers, you'll need to come up with a shim to provide support for Array.reduce(). MDN has one here:

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/Reduce

UPDATE:

Looks like I should have used filter() instead of reduce(). Makes the code much cleaner. Thanks to the OP for suggesting it!

var evens = range(0,21).filter(function (cur) { return cur % 2 === 0; });

Again, filter() is ES5, so you'll need a shim in order to ensure it will function properly on older browsers.

Pete
  • 2,538
  • 13
  • 15
  • 1
    +1 , but "JavaScript 1.7" isn't any kind of standard. JavaScript is Mozilla's proprietary implementation of ECMAScript for use in browsers. There isn't even a specification for it, the only documentation that comes close is the [on–line Reference](https://developer.mozilla.org/en-US/docs/JavaScript/Reference), which is maintained as a community wiki. – RobG Sep 24 '12 at 00:32
  • +1, thanks for all the information! I would use `filter` instead of `reduce` for the ES5 variant. But if I'm working on a project that I'm fine with being FF only, I'm also fine using JS1.7 features. – Nathan Wall Sep 24 '12 at 00:45
  • @NathanWall I will have to check out `filter()`. I am still learning all the ES5 stuff. ;-). Thanks for the feedback! – Pete Sep 24 '12 at 00:48
2

This type of statement is known as a list comprehension. Python has good examples with very similar syntax.

matt b
  • 138,234
  • 66
  • 282
  • 345