Immutability in functional programming
In traditional programming, data is often mutable, meaning that it can be manipulated and altered at will after it is created. Let's look at an example:
let arr = [1, 2, 3]; arr.push(4); // `arr` is now [1, 2, 3, 4]
In functional programming, however, data is treated as immutable, meaning it cannot be changed once it is created. Instead of modifying data, new data is created based on the old data. This helps prevent unintended consequences and makes it easier to reason about the program.
const arr = [1, 2, 3]; const newArr = [...arr, 4]; // `newArr` is [1, 2, 3, 4], `arr` is still [1, 2, 3]
As you can see in the example above, the original array is not modified. Instead, a new array is created based on the old array. The original array is still available and can be used elsewhere in the program, if needed.