Skip to main content

Command Palette

Search for a command to run...

Currying In JavaScript

Updated
โ€ข1 min readโ€ขView as Markdown
Currying In JavaScript
Y

An engineer by profession and a JavaScript Lover by heart.

First and foremost, I love writing code. Ever since writing my first program in C and manipulating it to produce the desired output. I believe in the power of programming to transform and improve the lives of people around the world.

My curiosity levels are as fresh as when I was a child. I believe in eternal learning and deliberate effort as they are the only way to become the smartest in the room. I am a good timekeeper, always willing to learn new skills and use them in real-life problems.

An ambitious individual with a desire to succeed. A Cricket fanatic. A student who likes to take risks and does not shy away from experimenting with various combinations in life. Striving to do a lot. Wish me good luck ๐Ÿ™๐Ÿผ

My primary interest is in Web Development and Mobile Application Development.

Tech Stack:- ReactJS, NextJS, NodeJS, MongoDB, GraphQL, Javascript Version Control:- Git, Gitlab

Currying is the process of converting a function that takes multiple arguments at once into a sequence of function's each with a single argument.

๐—›๐—ผ๐˜„ ๐—ฑ๐—ผ๐—ฒ๐˜€ ๐—ถ๐˜ ๐˜„๐—ผ๐—ฟ๐—ธ?

Let's say you have a function with multiple arguments: sum(a,b,c).

๐—จ๐˜€๐—ถ๐—ป๐—ด ๐—–๐˜‚๐—ฟ๐—ฟ๐˜†๐—ถ๐—ป๐—ด, ๐˜†๐—ผ๐˜‚ ๐˜„๐—ถ๐—น๐—น ๐—ด๐—ฒ๐˜ ๐—ฎ ๐—ณ๐˜‚๐—ป๐—ฐ๐˜๐—ถ๐—ผ๐—ป ๐˜€๐˜‚๐—บ๐Ÿฌ(๐—ฎ) ๐˜๐—ต๐—ฎ๐˜ ๐—ฟ๐—ฒ๐˜๐˜‚๐—ฟ๐—ป๐˜€ ๐—ฎ ๐—ณ๐˜‚๐—ป๐—ฐ๐˜๐—ถ๐—ผ๐—ป ๐˜€๐˜‚๐—บ๐Ÿญ(๐—ฏ) ๐˜๐—ต๐—ฎ๐˜ ๐—ฟ๐—ฒ๐˜๐˜‚๐—ฟ๐—ป๐˜€ ๐—ฎ ๐—ณ๐˜‚๐—ป๐—ฐ๐˜๐—ถ๐—ผ๐—ป ๐˜€๐˜‚๐—บ๐Ÿฎ(๐—ฐ).

๐—œ๐—ป ๐—ฆ๐—ถ๐—บ๐—ฝ๐—น๐—ฒ ๐—ง๐—ฒ๐—ฟ๐—บ๐˜€: ๐˜€๐˜‚๐—บ(๐—ฎ, ๐—ฏ, ๐—ฐ) โ€”> ๐˜€๐˜‚๐—บ๐Ÿฌ(๐—ฎ) => ๐˜€๐˜‚๐—บ๐Ÿญ(๐—ฏ) => ๐˜€๐˜‚๐—บ๐Ÿฎ(๐—ฐ)

๐—˜๐˜…๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ ๐Ÿ‘‡

const sum = (a) => {
 return (b) => {
  return (c) => {
   return a + b + c;
  };
 };
};

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

๐—›๐—ผ๐˜„ ๐˜๐—ผ ๐—ฐ๐—ฎ๐—น๐—น ๐˜๐—ต๐—ฒ ๐—ณ๐˜‚๐—ป๐—ฐ๐˜๐—ถ๐—ผ๐—ป?

๐Ÿฌ) sum(10)(20)(30); // 60
๐Ÿญ) const addTen = sum(5)(5);
addTen(20); // 30

More from this blog

Yash Mantri

20 posts

An engineer by profession and a JavaScript Lover by heart