Functional JavaScript Introduction and Resources

When programmers talk about functional programming, they are generally talking about two different but related things:

  • A programming style that emphasizes the intentional avoidance of side effects as well as hidden inputs and outputs.

  • A set of programming functions — such as curry, map, and filter — derived from lamba calculus.

These concepts are related because the types of functions associated with functional programming enable a greater degree of code abstraction by performing powerful and complex mathematical computations.

What Makes Functional Programming Functional?

Every function takes in one or more inputs and produces one or more outputs. Take this simple function for instance:

function example( a ) {
  var result = a + 1;
  return result;
}

This simple function takes a single input, a, adds one to that value, and then stores the result in a variable called result. In this case, a is the input and result is the output.

Simple enough, right?

However, what about this slightly more complex function? What are its inputs and outputs?

function example( a ) {
  var result = doSomething( a ) + 1;
  return result;
}

Well, now suddenly things are more complex. We have a function, doSomething(), nested inside of our function and acting on our variable. If you were to change doSomething() you would also necessarily change the result of example().

In this case, we have what is called a hidden input. Whatever it is that doSomething() actually does in an input into our example() function, and yet there's no way to tell by looking at our function exactly what it will do.

In addition, we would say that the function doSomething() has side-effects. What this means is that when doSomething() is changed it affects other functions. The entire scope of the function is not contained within the function.

Functional programming is a style of programming that tries to eliminate all side effects as well as hidden inputs and outputs by writing functions that explicitly declare all of their inputs and outputs.

For example, we could rework the function above in the functional style by keeping the functions seperate and declaring doSomething(a) as an input into example(a), like this:

function doSomething( a ){
  var b = a + 1;
  return b;
}
function example( c ){
  var result = c + 1;
  return result;
}
var c = example( doSomething(1) );

While this may look more complex at first glance, you can easily look at the line declaring var c and know that there are two functions involved in creating the output, whereas the previous example obscured that information.

If you're still a bit confused about exactly what functional programming is, What Is Functional Programming? by Kris Jenkins is an excellent technical introduction to this topic that applies to any programming language.

Functional Programming with JavaScript

Is JavaScript a functional programming language? Well, the answer to that question depends on what you mean by functional programming language.

You can write code using a functional programming style with JavaScript. However, plain vanilla JavaScript does not include the sort of powerful mathematical functions most programmers are looking for when they talk about functional programming.

The solution, of course, is to build or include a JavaScript library of functional programming functions. Do that, and JavaScript becomes a great language for writing functional code.

If you're primarily concerned with learning the functional programming style and less concerned with complex mathematical functions, then the following resources will help you better grasp the functional programming style and how it can be applied to JavaScript:

Functional JavaScript

While JavaScript can be used to program in a functional style, as we mentioned it does lack a library of the sort of functions many functional programmers are looking for.

In 2007, Oliver Steele released Functional — a JavaScript library that introduces the sort of mathematical formulas and functions that functional programmers frequently use.

Rather than force any constraints upon programmers forcing them to write functional code, Functional is basically a set of lambda calculus formulas converted into JavaScript functions.

  • High-order functions included in Functional include map, reduce, and filter.

  • Function-level methods include curry, partial, compose, guard, and until.

  • In addition, Functional includes support for lambda expressions.

Functional is hosted at GitHub and is completely free and open source. However, it hasn't seen a significant update since 2007. In other words, you can expect some bumps along the way if you try to make use of the old library.

If you want to learn more about Functional check out the following resources:

Functional JS

If the idea of using a circa-2007 JavaScript library makes you a little nervous — as it should — why not spin up a more modern alternative?

Functional JS by Lee Crossly was released in late 2013. It is similar to Functional by Oliver Steele in that it is basically a collection of calculus functions rather than constraints to enforce a functional style of programming. However, it includes a much wider range of functions including curry, arity, lambda, map, reduce, select, partition, group, and a whole lot more.

Functional JS is hosted at GitHub, and the official project website offers excellent and thorough library documentation. Although, you should be warned that the documentaiton is written in a style that assumes it is being read by experienced developers.

Summary

Functional programming is a style of programming developed in support of lambda calculus that encourages the explicit declaration of all inputs and outputs and the avoidance of side effects. A functional programming style can be used when coding with JavaScript.

However, standard JavaScript does not include the sort of powerful mathematical functions that many functional programmers are looking for. JavaScript can be enhanced to better suit a functional programming style by making use of free JavaScript libraries such as Functional by Oliver Steele and Functional JS by Lee Crossley.


Further Reading and Resources

We have more guides, tutorials, and infographics related to coding and development:

What Code Should You Learn?

Confused about what programming language you should learn to code in? Check out our infographic, What Code Should You Learn?

It not only discusses different aspects of the languages, it answers important questions such as, "How much money will I make programming Java for a living?"