Developers: Introduction To Functional Programming: Javascript Paradigms
Developers: Introduction To Functional Programming: Javascript Paradigms
Developers: Introduction To Functional Programming: Javascript Paradigms
® Developers
Engineering
authors are vetted experts in their fields and write on topics in which they have demonstrated
experience. All of our content is peer reviewed and validated by Toptal experts in the same field.
By continuing to use this site you agree to our Cookie Policy. Got it
https://www.toptal.com/javascript/functional-programming-javascript 1/16
2/15/24, 11:07 PM Intro to Functional Programming: JavaScript Paradigms | Toptal®
By Avi Aryan
Verified Expert in Engineering
Avi is a full-stack developer skilled with Python, JavaScript, and Go and is also a
multiple-time Google Summer of Code participant.
EXPERTISE
JavaScript
PREVIOUSLY AT
SHARE
Pure functions take some input and give a fixed output. Also, they cause no
side effects in the outside world.
By continuing to use this site you agree to our Cookie Policy.
https://www.toptal.com/javascript/functional-programming-javascript 2/16
2/15/24, 11:07 PM Intro to Functional Programming: JavaScript Paradigms | Toptal®
Here, add is a pure function. This is because, for a fixed value of a and b ,
the output will always be the same.
getId is not a pure function. The reason being that it uses the global
variable SECRET for computing the output. If SECRET were to change, the
getId function will return a different value for the same input. Thus, it is
not a pure function.
let id_count = 0;
const getId = () => ++id_count;
This is also an impure function, and that too for a couple of reasons—(1) it
uses a non-local variable for computing its output, and (2) it creates a side
effect in the outside world by modifying a variable in that world.
What’s the current value of id_count ? Which other functions are modifying
id_count ? Are there other functions relying on id_count ?
https://www.toptal.com/javascript/functional-programming-javascript 3/16
2/15/24, 11:07 PM Intro to Functional Programming: JavaScript Paradigms | Toptal®
2. Use pure functions: fixed output for fixed inputs, and no side effects
https://www.toptal.com/javascript/functional-programming-javascript 4/16
2/15/24, 11:07 PM Intro to Functional Programming: JavaScript Paradigms | Toptal®
Filter
array.filter(condition);
The condition here is a function that gets each item of the array, and it
should decide whether to keep the item or not and return the truthy boolean
value for that.
Map
map maps each item of array to a function and creates a new array based on
the return values of the function calls.
array.map(mapper)
mapper is a function that takes an item of an array as input and returns the
output.
Reduce
https://www.toptal.com/javascript/functional-programming-javascript 5/16
2/15/24, 11:07 PM Intro to Functional Programming: JavaScript Paradigms | Toptal®
array.reduce(reducer);
reducer is a function that takes the accumulated value and the next item in
the array and returns the new value. It is called like this for all values in the
array, one after another.
Concat
concat adds new items to an existing array to create a new array. It’s
different from push() in the sense that push() mutates data, which makes
it impure.
Object.assign
Object.assign copies values from the provided object to a new object. Since
functional programming is predicated on immutable data, we use it to make
new objects based on existing objects.
By continuing to use this site you agree to our Cookie Policy.
https://www.toptal.com/javascript/functional-programming-javascript 6/16
2/15/24, 11:07 PM Intro to Functional Programming: JavaScript Paradigms | Toptal®
With the advent of ES6, this can also be done using the spread operator.
We can create our pure function as well. Let’s do one for duplicating a string
n number of times.
duplicate('hooray!', 3)
// hooray!hooray!hooray!
Higher-order Functions
https://www.toptal.com/javascript/functional-programming-javascript 7/16
2/15/24, 11:07 PM Intro to Functional Programming: JavaScript Paradigms | Toptal®
withLog HOF can be used with other functions as well and it works without
any conflicts or writing extra code. This is the beauty of a HOF.
withLog(hype)('Sale');
// calling hype
// Sale!!!
Currying
We can also reformat our curried function to look succinct. This is because
each level of the currying function call is a single line return statement.
Therefore, we can use arrow functions in ES6 to refactor it as follows.
Composition
https://www.toptal.com/javascript/functional-programming-javascript 9/16
2/15/24, 11:07 PM Intro to Functional Programming: JavaScript Paradigms | Toptal®
The first function is range, which takes a starting number a and an ending
number b and creates an array consisting of numbers from a to b .
Then we have a function multiply that takes an array and multiplies all the
numbers in it.
Concluding Words
We hope that this piece piques your interest in functional programming and
possibly motivates you to try it in your code. We are positive that it will be a
learning experience and a milestone in your software development journey.
What is ES6?
https://www.toptal.com/javascript/functional-programming-javascript 11/16
2/15/24, 11:07 PM Intro to Functional Programming: JavaScript Paradigms | Toptal®
Avi Aryan
Verified Expert in Engineering
Avi is a full-stack developer skilled with Python, JavaScript, and Go and is also a multiple-time Google
Summer
By of Code
continuing participant.
to use this site you agree to our Cookie Policy.
https://www.toptal.com/javascript/functional-programming-javascript 12/16
2/15/24, 11:07 PM Intro to Functional Programming: JavaScript Paradigms | Toptal®
authors are vetted experts in their fields and write on topics in which they have demonstrated
experience. All of our content is peer reviewed and validated by Toptal experts in the same field.
EXPERTISE
JavaScript
PREVIOUSLY AT
Hire Avi
TRENDING ARTICLES
ENGINEERING › TECHNOLOGY
JavaScript Developers
By continuing to use this site you agree to our Cookie Policy.
https://www.toptal.com/javascript/functional-programming-javascript 13/16
2/15/24, 11:07 PM Intro to Functional Programming: JavaScript Paradigms | Toptal®
Sign Me Up
Toptal Developers
https://www.toptal.com/javascript/functional-programming-javascript 14/16
2/15/24, 11:07 PM Intro to Functional Programming: JavaScript Paradigms | Toptal®
Hire a Developer
or
Apply as a Developer
Application Services
START TO FINISH SOLUTIONS
Data Analytics Consulting
https://www.toptal.com/javascript/functional-programming-javascript 15/16
2/15/24, 11:07 PM Intro to Functional Programming: JavaScript Paradigms | Toptal®
ABOUT CONTACT
Top 3% Contact Us
About Us
https://www.toptal.com/javascript/functional-programming-javascript 16/16