Monday, February 4th, 2008

Functional Programming with JavaScript and Dojo

Category: Dojo, JavaScript

Eugene Lazutkin has written a piece on Functional fun in JavaScript with Dojo where he delves into the land of functional and how it is available in JavaScript.

Eugene maps out some of the helpful functions that JavaScript itself has added over time:

  • JS 1.6 (in Firefox 1.5) introduced
    so-called Array extras:
    special Array methods, which help to simulate lists with arrays:
    indexOf(), lastIndexOf(), every(), some(), filter(), map(),
    forEach(). The last five methods are especially important because
    they help to eliminate the most common direct loops.
  • JS 1.7 (in Firefox 2) introduced Array comprehensions borrowed from Python. The new syntax allows to generate arrays using
    a compact yet clear notation reducing the possibility of errors. And
    of course iterators and generators will helps us with cleaner loops
    too. Another goody is the block scope with “let”.
  • JS 1.8 (in Firefox 3) brought us
    more Array extras:
    reduce() and reduceRight(). They give us a native support for
    all-important folds. Another notable additions are expression
    closures (simplified one-line functions), and generator expressions.
  • JS 2 (ES4 PDF)
    takes us even farther: for each statement, tail calls, and the whole
    raft of language improvements. Presumably JS 2 will come with the
    next generation of JavaScript virtual machines helping to reduce
    penalties for using new abstractions.

… and how Dojo implements many so you have cross browser access.

He goes into detail on his favourite five: filter(), map(), forEach(), every(), and some().



  1. var percents =, function(val){ return val / sum; });
  3. var sum = 0;
  4. dojo.forEach(values, function(val){ sum += val; });

Next he goes beyond core to dojox.lang.functional where lambda is your friend:


  1. var div2 = df.lambda("/2");

What about performance? We get a nice run down on the performance of the Dojo functions compared to native ones if they are available.

Very thorough indeed.

Posted by Dion Almaer at 8:18 am

3.7 rating from 30 votes


Comments feed TrackBack URI

This is good, but I don’t see a way to break from dojo.forEach, which is available in Prototype.

Comment by Les — February 4, 2008

Hi Les–feel free to file an enhancement ticket at (login as guest/guest) if you’d like to see us add that feature. My understanding is that dojo.forEach was modeled on the Array Extras, and off the top of my head I don’t believe there’s a way to break from that function either (though I could definitely be wrong).

Comment by Tom Trenka — February 4, 2008

I did submit a reduce implementation to dojo a while back:

But don’t know if it survived the Great Reorg for 0.9

Comment by gavindoughtie — February 4, 2008

Thank you Eugene! It is really what JS community needs!

To the rest of dojo team: Guys, the documentation is your advertising. Newcomers will not consider that you were almost THE_FIRST framework outhere. Rare teamlead/architect will approve using of framework with absent tutorials and API doc. Wake up! Old good days passed. Now you have to compete. No offenses, but compare your doco with

Comment by Robert — February 4, 2008

@Les: I think the goal is to be functionally compatible with the spec’d Mozilla extensions for direct replacement with the native methods, where available. In that spirit, I think the solution is to use Array.every (aka dojo.every) if you wish to break out of the loop.

Comment by peller — February 4, 2008

@Les: personally I use every() or some() to simulate the breaking out of loop:

dojo.some(array, function(val){
// do something useful
return criterium(val); // break if true

Comment by elazutkin — February 4, 2008

@Les as @elazutkin said, using return is a good bet.

Within a function used by forEach() you can ‘return’ to simulate a continue:

var aList = new Array(1,2,3,4);
dojo.forEach(aList,function(nArg) {
if ( nArg == 2 ) return;
// outputs 1,3,4

You could add some more code to simulate break, but by the time you do that one has to wonder whether it’s worth using the forEach construct in the first place.

Comment by peteotaqui — June 17, 2008

Leave a comment

You must be logged in to post a comment.