### Functional Programing in Javascript

Perm url with updates: http://xahlee.org/js/javascript_lang_fun.html

# Functional Programing in Javascript

Xah Lee, 2009-01-28, 2010-10-30

This page shows you some functional programing examples in javascript. If you are not familiar with js, see: Javascript Basics.

#### Defining a Function

Here's how to define a function:

// defines a function function f(n) {return n+1;};

#### Function without “return” returns “undefined”

If you want your function to return a value, you must use the “return” statement. A function without return value, when called, will return the builtin special value “undefined”. Example:

// defines a function function f(n) {n;}; // call the function f alert(f(3)); // result is “undefined”

#### Pure Function (aka Anonymous Function, “lambda”)

You can define a function without naming it. Example:

// defines a function without naming it function (n) {return n+1;};

#### Function's Value is “function”

A function definition has a value. Its value is a special value of type “function”.

alert( typeof( function f() {return 1;} ) ); // prints “function”

#### Assign a Function to Variable

You can assign a function to a variable.

// assign a function to a variable var f = function (n) {return n+1;}; alert(f(2)); // prints 3

The above is effectively equivalent to this:

function f(n) {return n+1;};

#### Apply a Function to a Value

You can apply a function to value directly.

// apply a pure function to value directly function (n) {return n+1;} (2); // result value is 3

#### Function Returning a Function

You can have a function return another function.

// function returning a function function f(n) { return function (x) {return (n.toString() + " and " + x.toString() ); }; } alert (f(2) (7)); // prints “2 and 7”

So, for example, you can define a function “f(n)” that returns a function “f(x)” that computes nth power of x.

// function returning a function function f(n) { return function (x) {return Math.pow(x,n); }; } alert (f(2) (7)); // prints 49

In the above, we first call 「f(2)」, the result is a function that computes x^2. Then, we give this function a argument of 7, so the result is 49.

#### Recursion

Function can call itself. Here's a example of factorial.

function f(n) { if (n < 0) { return -1; } if (n == 0) { return 1; } else return (n * f(n - 1)); } alert(f(4)); // prints 24. (it's 4×3×2×1)

#### Function Composition

Because all the above features, javascript has some ability to do advanced functional programing. For example, we can define a function (and we call it simpleFunctionCompose), that takes 2 arguments, each is a function (call them f and g), and simpleFunctionCompose returns a new function whose behavior is equivalent to “f(g(x))”.

// function composition // takes two single value functions f(x) and g(x) and returns a // function that computes f(g(x)) function simpleFunctionCompose(f, g) { return function (n) { return f(g(n)); }; } // test function h(n) { return n+1;} function i(n) { return n*2;} alert(simpleFunctionCompose(h,i)(3)); // prints 7