Devin Roché

JavaScript & Currying

Currying was something that went over my head, but I wish I knew. It wasn’t until I was approached with a problem.

THE PROBLEM: Write a function the can perform the following, add3(1)(2)(3).

What is currying? Currying is when we take function and break it down to a sequence of functions that takes only small parts of the argument.

Why use it?

  • easier to reuse
  • anonymous
  • don’t need all params at once

Back to our problem! We can solve this using the following:

add3 = a => b => c => a + b + c

Then we can call it a handful of ways:

  • add3(1)(2)(3)
  • add3(1)(2) & add(3)
  • add3(1) & add3(2) & add(3)

twitter |github