Currying is a powerful pattern in the Javascript language. Many libraries make use of this pattern. It is primarily based on the concept of closures. If you understand scopes and closures well, it would be easy to understand.

It's a must read for anyone preparing for a front-end/javascript interview. Questions around currying in javascript are highly expected in an interview. So don't miss it.

In this post, we'll discuss multiple javascript interview problems around currying - from a simple one to the advanced ones.

Let's start with a simple sum function. All it does - takes 3 arguments, adds them up and returns the sum.

function sum (a, b, c) {
  return a + b + c;
}

sum(5, 6, 7);

Problem

Can you rewrite the sum function to take arguments like the one shown below?
sum(5)(6)(7);

Wow! that's a weird way to invoke a function. Isn't it?

It's called a curried version of the sum function.

A lot of candidates get confused when they see problems involving currying. Even the experienced ones. But it's not as hard as it seems.

Experience Level

Questions around currying are generally asked to candidates with 2 years+ experience, mid to senior level Javascript developers.

Concepts Used

You need to know scopes and closures in Javascript to understand currying.

Solution

The solution is easy. Have a look at the below code:

function sum(a) {
  return function(b) {
    return function(c) {
      return a + b + c;
    }
  }
}

Currying in ES6

The ES6 version is a one liner! Fantastic... Isn't it?

const sum = a => b => c => a + b + c;

Solution Explanation

Functions in Javascript are just objects. A function can be returned from another function just like an object or some value. To execute it you need to invoke it using ().

When you call sum(5), it returns the first inner function. This function has been returned but it will still have access to value of a which is 5 in this case. How? Because of a closure.

Now when we add another () to sum(5) -> sum(5)(6), the returned first inner function will get executed and will return the second or the innermost function. The innermost function is again a closure and it will have access to the parameters a and b passed to the two outer functions.

Now we finally add another () to  sum(5)(6) -> sum(5)(6)(7). This will now invoke the innermost function. We pass 7 as an argument to it. It remembers a and b because of closure.

So, the result we get is 5 + 6 + 7 = 18

Level Up

Your interviewer would generally start with the problem we just solved, but they won't stop there. The currying problem we discussed above is just to understand if you know about the inherent concepts.

There are chances that there will be advanced follow up questions. Like the one below:

Ok. I have a curried version of sum now that takes 3 arguments. Now I want similar functionality be extended to other operations like multiply. Number of arguments would always be 3.
mul(5, 6, 7) -> 210

Okay, that's easy. Here you go:

function mul(a) {
  return function(b) {
    return function(c) {
      return a * b * c;
    }
  }
}

Don't do that. It is correct, but not what your interviewer expects here. They want to see if you can generalise the  solution.

Here's what your solution should look like:

function curry(fn) {
  return function(a) {
    return function(b) {
      return function(c) {
        return fn(a, b, c);
      }
    }
  }
}

var sum = curry(function(a, b, c) {
  return a + b + c;
});

var mul = curry(function(a, b, c) {
  return a * b * c;
});

sum(5)(6)(7); -> 18
mul(5)(6)(7); -> 210

So, you create a curry function. The curry function takes in the core function responsible for performing the actual operation. It then accumulates the params through a series of inner functions. In the innermost function, it finally passes those accumulated params down to the core function. And that's it - a generalised curried function.

Feel like a Pro? Don't yet. The hardest part is yet to come.

The Hardest Part

Ok. Now extend the curried function to n number of arguments.

We'll discuss it in another post - Javascript advanced curry interview questions.