![]() |
VOOZH | about |
Functional Programming(FP) is as old as programming but some of us think that it is discovered recently because we found some parts in mathematics but not in the programming world. But nowadays functional programming is in trend. Almost every programming language including Java, Python, JavaScript, etc. is adopting functional programming. It seems like functional programming is the mainstream now. But why everyone is focusing on functional programming? There must be something. The right question to ask is Why Functional Programming? So let us explore.
There are two types of programming in general.
Imperative: With imperative programming, our code tells the compiler and users about how to do a task.
Example:
Output:
[2, 4, 6, 8]
Declarative: With Declarative programming, our code tells the compiler and users what to do.
Output:
[2, 4, 6, 8]
It is highly likely that for most programmers, the Imperative code looks more familiar and more readable than the declarative code. This is because we preferably learn Imperative code first. But most of you don't know what declarative code brings to us. Declarative code comes with great benefits
The next big question comes why JavaScript? It could be because of reach. We all know JavaScript is the most popular programming language. It could also be because you want to code both client-side and server-side. If you want to choose because you want to learn functional programming then you've chosen the best one because JavaScript supports functional programming "out of the box". The main idea of functional programming revolves around functions. In JavaScript, functions are first-class citizens i.e values. We can pass it as an argument, return from a function, and store it in a variable. JavaScript is a very flexible language. We can call a function with any number of arguments of any type and at any time.
The key takeaway is that we should learn functional programming to make our code more expressive, robust, and more readable.