Javascript Jems - Lambda expressions
Written by Ian Elliot   
Tuesday, 14 January 2014
Article Index
Javascript Jems - Lambda expressions
Using anonymous functions and invocation

A more familiar alternative

Things are even simpler because you don't have to store a reference to a function to pass it to another function or to make use of it. 

In other languages this facility is often called an anonymous function. In JavaScript is is just a use of the idea of an object literal i.e an object crated on the fly as in {myProperty:"value"}.

Anywhere you can use an object reference you can use an object literal. 

JavaScript also supports anonymous functions as object litterals and you can write this sort example as:

             return a.length-b.length;});

This last example looks a lot more like the sort of typical use that lambda expression are put to in other languages.

In say C# you could write the same thing using

list.sort((a,b) => a.length-b.length)

and the expression (a,b) => a.length-b.length is a lambda expression. Notice that apart from being a little more compact - no need for the keywords function and return - it is more or less the same. 

Thus JavaScript with its first class functions or functions as objects doesn't really need lambda expressions or it already has them depending on your point of view.




Of course there is nothing stopping you from creating your own functions that accept other functions. As long as you have fully accepted the "functions are just objects" idea you probably don't need to see an example of how to do this but for completeness:


This will simply call any function that you pass to it. If you define:




will call the Hello function and display an alert.

Notice that you have to pass the variable that references the function without accidentally invoking the function.

That is  don’t write:


by mistake as this would call the Hello function and then pass its result to the Say function which as a string couldn't be called as a function.

You need to distinguish very clearly between passing a function and passing the result of a function to another function.

Function Invocation ()

You also need to get into the habit of thinking of the () brackets as being the function invocation operator. When ever you use () following an expression it calls the function that is the result of the expression.

For example you can write:


or even:


The round brackets at the end of the function definition call the function as soon as it has been defined. 

This is usually called an "Immediately Invoked Function Expression" or IIFE and it is very useful and used a lot in idiomatic JavaScript.

This instant function evaluation can be useful when you want to pass the result of a "lambda expression".

For example:

alert(function sum(a,b){return a+b;}(1,2));

Here we have a lambda expression, the sum function being defined and evaluated in a single step.

The result, 3 in this case is passed to the alert function.

Although this example isn’t particularly useful it does become useful when combined with other Javascript features such as closure – more of which another time.


So what to take away -

In JavaScript functions are function objects and they can be created and used in the same way.

The only difference between an object and a function object is that a function object has a default method i.e. the code that is usually considered to be the function. 

Once you understand JavaScript's first class functions you realize that you don't need to introduce anything extra to the language such as lambda expressions, delegates or anonymous functions to do the same tasks. 

Superficially a function object literal looks a lot like a lambda expression and it can mostly be used in exactly the same way.

This means you can pass functions to other functions store them in arrays and generally make use of them as standard objects.

Related Articles

Javascript Jems - First class functions

Chaining - Fluent Interfaces In JavaScript

JavaScript Objects With Value - valueOf and toString

JavaScript Hoisting Explained       

WAT! JavaScript, Ignorance And Prejudice       

What Exactly Is A First Class Function - And Why You Should Care     

Lambda Calculus For Programmers       

The Undefined Defined Variable       


To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.






or email your comment to:



Just jQuery The Core UI -- Filters

Filters are often confused with selectors, but they are quite different and serve the important purpose of discovering the things that a simple selector cannot.

Just jQuery The Core UI - Modifying The DOM

jQuery provides you with methods for working with the DOM in ways powerful enough to allow you to create custom controls that extend what you can incorporate into an HTML page.  But first we must [ ... ]

Other Articles







Last Updated ( Tuesday, 28 October 2014 )