Functional programming is a programming paradigm — a style of building the structure and elements of computer programs — that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data
(a block of code ) that always returns the same result if the same arguments are passed. It does not depend on any state, or data change during a program’s execution rather it only depends on its input arguments
It returns the same result if given the same arguments (it is also referred as deterministic)
It does not cause any observable side effects
The code’s definitely easier to test. We don’t need to mock anything.
When data is immutable, its state cannot change after it’s created. If you want to change an immutable object, you can’t. Instead, you create a new object with the new value.
Basically, if a function consistently yields the same result for the same input, it is referentially transparent.
pure functions + immutable data = referential transparency
With this concept, a cool thing we can do is to memoize the function. Imagine we have this function:
const sum = (a, b) => a + b;
Just another JavaScript file that does a certain functionality of the code.
The require function is intended to add separate pieces of code (“modules”) to the current scope.
by export it then require it in the needed file.
Export it using module.exports.