Functional Programing in JavaScript

The programing techniques on this page are not JavaScript basics, but are not advanced “fancy tricks” neither. If you do any serious JavaScript programing, you have to know all of the techniques shown on this page.

JavaScript Function Techniques

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 with keyword return.

A function without return, when called, will return the builtin special value undefined. Example:

Function Composition

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