Sitemap
Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

Member-only story

Interview11: Curry function in JavaScript

2 min readSep 20, 2024

--

Press enter or click to view image in full size

In this article, we are talking about Curry Functions, and how to implement the Curry Functions in Javascript.

Currying in JavaScript is a functional programming technique that breaks down a function that takes multiple arguments into a series of functions that each take a single argument.

f(a, b, c) // Normal Function

f(a)(b)(c) // Curry Function

Let’s start with a simple curry function.

function curryFn (x) {
return function(y) {
return x + y;
}
}

console.log(curryFn(10)(20)); // Called twice with (10), with (20)

What if we want to pass n number of params like curryFn(10)(20)(30)(40)?

Infinite Currying

write a curry function to accept n number of params

function curryFn (x) {
var inner = function (y) {
if(!y) return x;
x = x + y;
return inner;
}

return inner;
}

console.log(curryFn(10)(20)(30)(40)(100)());

You may notice curryFn has been called with empty params in the last to identify the end of curryFn.

--

--

βœ… open src πŸš€ πŸš€
βœ… open src πŸš€ πŸš€

Written by βœ… open src πŸš€ πŸš€

1. Open source contributor, Architect at Jio Platform Ltd. and The Author of fe-pilot. https://github.com/opensrc0/fe-pilot/

Stackademic
Stackademic

Published in Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.