Functional Programming
Functional programming is a deep topic and the subject of many books by itself. This book can only touch on the topic. One of the foundational concepts in functional programming is to use simple functions that have an input and an output and do not modify variables that are outside their scope:
Example_Functional_1.ts
1 let importantNumber = 3; 2 3 const addFive = (): void => { 4 importantNumber += 5; 5 }; 6 7 addFive(); 8 9 console.log(importantNumber);
Link to the preceding example: https://packt.link/CTn1X
The function produces the following output:
8
The output of this program is correct. We have indeed added 5
to the initial value of 3
, but the addFive
method accesses a variable in a higher scope and mutates it. It is greatly preferred in functional programming paradigms to instead return the new value and allow the outer scope to control the variables that have been declared in it. We can change addFive
so that it no longer operates...