DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on • Updated on

Currying in Javascript

WHAT IS CURRYING IN JAVASCRIPT?

Currying in JavaScript transforms a function with multiple arguments into a nested series of functions, each taking a single argument. Currying helps you avoid passing the same variable multiple times, and it helps you create a higher order function.

OR

Translating the evaluation of a function that takes multiple arguments (or a tuple of arguments) into evaluating a sequence of functions, each with a single argument (partial application)

Why Is Currying in JavaScript Useful?

  • Currying helps you avoid passing the same variable again and again.

  • It helps to create a higher order function.

  • Currying transforms a function with multiple arguments into a sequence/series of functions, each taking a single argument.

  • Currying allows greater code clarity in certain cases. What does clarity mean? Reading the function provides clear indication of its functionality.

  • Currying can help to eliminate redundant code by allowing developers to create specialized functions that share common functionality. This can lead to a more streamlined code base that is easier to maintain.

I can't think of an instance where currying—by itself—is useful in JavaScript; it is a technique for converting function calls with multiple arguments into chains of function calls with a single argument for each call, but JavaScript supports multiple arguments in a single function call.


// Here are some examples:
// Using Closure
// Example: 

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


// Using Bind
// Example: 
function sum(a, b, c) {
  return a + b + c;
}

const curriedsum = sum.bind(null, 1);

console.log(curriedsum(2, 3)); // outputs 6

// Using Lodash (Third Party Library)
// Example: 
const _ = require('lodash');

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

const curriedsum = _.curry(sum);

console.log(curriedsum(1)(2)(3)); // outputs 6
console.log(curriedsum(1, 2)(3)); // outputs 6
console.log(curriedsum(1)(2, 3)); // outputs 6

// Other Example
// Example: 
function Welcome(name) {
  var greetingInfo = function (message) {
    console.log(message + " " + name);
  };
  return greetingInfo;
}

var myFunction = Welcome("John");
myFunction("Welcome "); //Output: Welcome John
myFunction("Hello Mr."); //output: Hello Mr.John


// Example:
const ob = {
  name:"zeeshan",
  age:28
}

function userInfo(obj){
  return function userObj(prop) {
    return obj[prop];
  }
}
console.log(userInfo(ob)('name'))


const multiArgFunction = (a, b, c) => a + b + c;
console.log(multiArgFunction(1, 2, 3)); // 6

const curryUnaryFunction = (a) => (b) => (c) => a + b + c;
curryUnaryFunction(1); // returns a function: b => c =>  1 + b + c
curryUnaryFunction(1)(2); // returns a function: c => 3 + c
curryUnaryFunction(1)(2)(3); // returns the number 6


Enter fullscreen mode Exit fullscreen mode

Top comments (0)