The above definition is taken from Wikipedia, now let’s try to understand the value and benefits of FP(functional programming) in this article.
The first fundamental concept we learn when we want to understand functional programming is pure functions. But what does that really mean? What makes a function pure?
So how do we know if a function is
pure or not? Here is a very strict definition of purity:
In mathematics, a function is a relation between a set of inputs and a set of permissible outputs with the property that each input combination is related to exactly one output.
In functional programming, these kinds of functions called pure functions which only depends on the received input data to the function and does not alter the input data except the returned data.
Math.random() is not pure function because it always returns new value on each call.
Math.min(1,2) is an example of pure function which always returns same value with same set of inputs
Immutability is important to make sure one function does not change the original data rather than should return new copy of the data after manipulation.
Like if Array and Objects are passing around multiple functions and we are not maintaining immutability then functions might not get the original copy of the array and object.
It is really hard to debug if something goes wrong in case of mutable object and array.
Mutability is not bad but it should be when to have it.