DEV Community

John Au-Yeung
John Au-Yeung

Posted on • Originally published at thewebdev.info

Functional JavaScript — Benefits

Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62

Subscribe to my email list now at http://jauyeung.net/subscribe/

JavaScript is partly a functional language.

To learn JavaScript, we got to learn the functional parts of JavaScript.

In this article, we’ll look at how to use the functional programming features in JavaScript.

Functional Programming Benefits

Functional programming has various benefits.

This is why it’s being adopted into programming languages like JavaScript.

Pure Functions

One benefit of functional programming is that we define pure functions in our code.

A pure function returns the same output if we pass in the same input to it.

For instance, we can write:

const square = (value) => value ** 2;
Enter fullscreen mode Exit fullscreen mode

We get the value and we return the square of it.

This doesn’t change regardless of what happens outside.

The benefit is that pure functions can easily be tested.

We can just check the output after giving it some input.

Since it doesn’t depend on anything outside, we can check for it easily.

We can check the returned value by writing something like:

square(2) === 4
Enter fullscreen mode Exit fullscreen mode

Reasonable Code

It’s easy to read the code since the code for the function all reside inside the function.

For example, if we have:

const square = (value) => value ** 2;
Enter fullscreen mode Exit fullscreen mode

All we did is square the number which is passed in.

There’s nothing outside, so we can look at it easily.

Parallel Code

Since pure functions don’t depend on any values outside the function, we don’t have to worry about synchronize our function’s value with something outside.

If we have global values, then we may have to do something like this:

let global = "something"
let foo = (input) => {
  global = "somethingElse"
}

let bar = () => {
  if (global === "something") {
    //...
  }
}
Enter fullscreen mode Exit fullscreen mode

We’ve to check the value for the global variable before doing something.

With pure function, we don’t have to do that since there are no external dependencies.

Cachable

Pure functions always return the same output for the given input.

So we can cache the function outputs easily.

We just use the input as the key and the output as the value.

We can just look up the value from the cache.

With caching, we can increase the speed of our code.

For example, we can keep a cache with an object:

const cache = {
  1: 2,
  3: 4,
  //...
}
Enter fullscreen mode Exit fullscreen mode

Then we can check or the cached value by writing:

const value = cache.hasOwnProperty(input) ?
  cache[input] :
  cache[input] = longRunningFunction(input)
Enter fullscreen mode Exit fullscreen mode

We check for the cached value before we run the longRunningFunction .

Pipelines and Composable

We can compose pure functions easily.

We just pass in the return value of one pure function to another pure function.

For example, we can write:

const foo = (a) => {
  return a * 2;
}

const bar = (b) => {
  return b * 3;
}
Enter fullscreen mode Exit fullscreen mode

We can compose the functions by writing:

foo(bar(100));
Enter fullscreen mode Exit fullscreen mode

It looks like a mathematical function and it’s a mathematical function.

Conclusion

Functional programming has various benefits.

We can run code in parallel easily since we don’t have to synchronous code.

Also, we can compose functions easily.

They’re also easier to read and test.

Top comments (0)