JavaScript Interview Question: What is Functional Programming
JavaScript Functional Programming with examples
TLDR;
Follow these two simple rules of functional programming to write the best possible JavaScript:
- Keep data immutable
- Keep functions pure — accept at least one argument, return data or another function
1. Immutability
To mutate is to change, so to be immutable is to be unchangeable. In a functional program, data is immutable. It never changes. Instead of changing the original data structures, we build changed copies of those data structures and use them instead
1.1. Immutable vs mutable Functions
To understand how immutability works, let’s take a look at what it means to mutate data. Consider an object that represents the color lawn:
We could build a function that would rate colors, and use that function to change the rating of the color object.