What Is Functional Programming In Javascript?

In this tutorial, you will gain an understanding of what functional programming is and how you can use some functional techniques to help you write more concise and easier to understand code.  Functional programming has been around a long time.  


Is Javascript A Functional Programming Language

Javascript is not a fully functional programming language, like Haskell.  Javascript treats functions as first-class citizens.  To be clear, when I say 'functions are first class citizens' this means that functions work the same way as variables.  This means functions can be assigned to variables, passed into functions as an argument and can also be returned as the result of a function.

As Javascript treat functions this way, there is a whole new work of design paradigms and tools you can add to your coding tool-book, like higher-order functions, currying, pipelining and composing.  A lot of popular frameworks like Redux, JQuery and Lodash all use functional programming techniques, so a lot of it might not be that new to you.

What Is Functional Programming?

In simple, terms - functional programming is all about writing very small and concise functions that do one thing well, and then chaining these functions together to do more powerful things.  This is a completely different way of coding to non-functional languages.   In a non-functional approach, you may use tools like conditional logic, inheritance, objects, and classes to build up the structure of your app.  For a simple example, if you had a function that contained a massive if/else statement, in a functional approach, you would refactor it, to remove the conditional statements and build one or more functions, that can be chained together to achieve the same logic.

When it comes to functional programming in Javascript, it is definitely not advised to use it as an all or nothing technique.  Using pure functional techniques in some instances can result in more complicated and harder to understand code.  In other situations, it can make your code more declarative (easier to understand).  

When it comes to Javascript, to gain a more in-depth understanding about how to architecture your code, you need to know about the different functional programming and you also need to learn in which situations you should apply them.




submit to reddit

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top