Is Javascript a Functional Programming Language?
Well to give you a short answer, it's complicated. you would find people that say that it is and it isn't. Today we will try to understand why this is a wrong question. It's not important whether Javascript is a functional language or not. The only thing which is important for a developer is to know what FP (i.e. functional programming) features javascript has and why you should use them in your project.
So, What is a functional programming paradigm? and why should you, as a developer care? What FP does is help create isolated components which ensure fewer bugs in a project. Now to understand FP, we should understand a few of the core concepts that FP language employs and also find out how javascript implements them:
- Pure functions - The idea here is that we want a function to produce the same output for the same input every time it is run. if a function returns the same result, then you can cache the result and return it whenever this function is called in the future. Opposite to this, we have impure functions which rely on some external state of the program that is not defined in the function. These kinds of functions are difficult to debug. A simple example would be:
// Pure Function
const func = (a, b) => {
return a+b;
}
// Impure Function
let c = 4;
const func2 = (a, b) => {
return a+b+c;
}
First-class function - In functional languages, functions are treated as any other value, and therefore we can pass functions as parameters to other functions and also return them as values. Functions that take other functions as arguments are called Higher-order functions. Using first-class functions along with recursion allows for a simple proof of correctness. Examples of higher-order functions in Javascript are
map
,filter
etc.Immutability - Immutability basically means that once a value is assigned to a variable, it can't be modified. Instead, when you modify the data, a new copy is created with the desired changes, leaving the original data unchanged. Although it seems inefficient to create new copies, functional programming languages often employ techniques like structural sharing or memoization to optimize performance. Some Javascript libraries such as React have immutability baked into their core principal.
Referential Transparency - If you can replace the value of a function call with its return value, everywhere in the program, without affecting the program, then it is said to be referentially transparent. Referentially transparent code is predictable and easier to understand because the function does not depend on any hidden state or external factors. This ensures straightforward testing where a module can be tested in isolation. Although Javascript does not enforce referential transparency by default, it allows developers to write referential transparent code if they choose to do so.
Lazy Evaluation - The idea of lazy evaluations is that expressions are not evaluated until their results are actually needed. Although Javascript does not implement lazy evaluation by default, some techniques and libraries can be used to achieve similar behavior in specific cases. Few such methods are
thunk
andmemoization.
Hopefully, now you understand the basic core principles of functional programming language and how Javascript employs those concepts. whether Javascript is a functional language or not? I leave that question to other pundits.
I hope that this makes you feel a bit more comfortable with the inner working of Javascript. It's natural to feel overwhelmed when learning new things. With time, you will surely understand and appreciate the nuances of Javascript. As always, you are also free to reach out to me if you have any questions!
Subscribe to my newsletter
Read articles from Pankaj Barnwal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Pankaj Barnwal
Pankaj Barnwal
I am a Full stack web developer currently working in a Fortune 500 Company. My work mostly revolves around Javascript, React, and Python.