To me, the best part of functional programming in the context of JavaScript is the ability to chain function calls. Immutability forces the implementation of functions to strictly return something given an input without any side effects. It is much more readable to see a chain of array methods rather than a bunch of for loops (bonus points if nested).
constsomeNums=[-1,-2,-3,4,5];// This function returns the sum// of the squares of the positive numbers// in an array.functiongetSum(nums){returnnums.filter(x=>(x>0)).map(x=>x**2).reduce((prev,curr)=>(prev+curr));}console.log(getSum(someNums));// 41
Pure functions + immutability helps a lot to compose functions (or in your words "chain function calls"). And software is all about solving small parts and compose all "solutions" to solve the complex problem (bigger problem).
I will definitely write a separate post about composition!
To me, the best part of functional programming in the context of JavaScript is the ability to chain function calls. Immutability forces the implementation of functions to strictly return something given an input without any side effects. It is much more readable to see a chain of array methods rather than a bunch of
for
loops (bonus points if nested).Exactly!
Pure functions + immutability helps a lot to compose functions (or in your words "chain function calls"). And software is all about solving small parts and compose all "solutions" to solve the complex problem (bigger problem).
I will definitely write a separate post about composition!
Looking forward to it! I'd love to see the different ways to compose functions.