What is function currying in JavaScript ?

What is function currying in JavaScript ?

Function currying is a way to change a function that takes multiple arguments, like sum(a, b), into a series of functions that each take one argument, like sum(a)(b).?

The end result is the same, but it can make the code more reusable and easier to understand.

Look at this simple example,

No alt text provided for this image

when we call the function sumCurry(2), it returns a closure. which is why the inner function which is returned from function sumCurry() can access the variable ‘a’ and adds it to param ‘b’ and return the result a+b when we invoke it (sumCurry(2)(3)).




So next time you find yourself writing a function that takes multiple arguments, consider using function currying to simplify your code and make it more flexible.

#javascript #javascriptdeveloper #webdevelopment #froend

Have you ever used function currying in your code? What were your experiences with it? Share your thoughts in the comments!!

Hello Siva... We post 100's of job opportunities for developers daily here. Candidates can talk to HRs directly. Feel free to share it with your network. Visit this link - https://jobs.hulkhire.com And start applying.. Will be happy to address your concerns, if any

回复
Siva Rajana

Software Engineer | IIIT-Nuzvid

1 年

Function currying is named after an American mathematician named Haskell Curry who developed the concept :)

要查看或添加评论,请登录

Siva Rajana的更多文章

  • Microfrontends

    Microfrontends

    GatsbyJS’s Founder Kyle Mathews made a profound observation recently, stating that, "Websites today are still made the…

    1 条评论
  • Web Accessibility: A Small Guide for Web Developers

    Web Accessibility: A Small Guide for Web Developers

    Web Accessibility refers to designing and developing websites, so that people with disabilities can use them without…

    1 条评论
  • Dancing with the Event Loop!!

    Dancing with the Event Loop!!

    Back in college, during our 3rd year of engineering, our batchmates were planning a farewell party for our…

    2 条评论
  • Closures and Cricket: A Lesson in Life and JavaScript

    Closures and Cricket: A Lesson in Life and JavaScript

    Once upon a time in the world of childhood, there was a passion that burned brighter than the scorching sun on a summer…

  • From Confusion to Clarity: Demystifying call, apply, and bind in JavaScript

    From Confusion to Clarity: Demystifying call, apply, and bind in JavaScript

    It had been a hectic day at the office, and I decided to sleep early. Suddenly, I felt like I couldn't breathe and was…

  • Unlocking Peak Performance in React Applications

    Unlocking Peak Performance in React Applications

    In today's web world, clients want more than just good-looking websites. They want websites that work fast and smoothly.

  • How is javascript code executed?

    How is javascript code executed?

    Javascript code will be executed in two phases whenever we try to run a javascript program. And in the first phase, the…

    4 条评论
  • How to sort an array of objects?

    How to sort an array of objects?

    Today I got a simple scenario where I have to sort objects in an array based on the value of a key present in each of…

  • Different ways of swapping elements in a JS Array

    Different ways of swapping elements in a JS Array

    I was working on a UI where a group of cards are getting rendered based on the data that is present in the array & I've…

    1 条评论
  • Why I Stopped Using “&&” for Conditional Rendering in React

    Why I Stopped Using “&&” for Conditional Rendering in React

    I have been using the #javascript '&&' operator for conditionally rendering my react components. The value of the…

    2 条评论

社区洞察

其他会员也浏览了