Functional Programming in Javascript 

Functional Programming in Javascript 

The mystic term of Functional Programming (FP) must be familiar to any JS developer. The first impression when we say “JS supports functional programming paradigm”.

The mystic term of Functional Programming (FP) must be familiar to any JS developer. The first impression when we say “JS supports functional programming paradigm” or “JS is a Functional programming language”, could be, well I use functions in JS, so I’m already doing Functional programming. This is the myth that we’re going to bust together.

You might be using the FP unknowingly but there’s a lot more to the world of Functional programming which takes years of experience to master. It involves re-wiring your thinking process to observe and implement the FP patterns in your style of writing programs.

Preface

Researching online about FP can leave you inundated with mathematical information that you have no awareness of. This is probably why it’s not that popular among the developer community (due to the steep learning curve involved). Even if you learn some of FP concepts in Javascript, like currying or composing, you may find yourself wondering “How the heck can I use it in my real-world projects?” That’s the same situation I found myself in when I learned about currying (which I wrote an article two years back here wherein I explained it with the help of a contrived example). Guess what, the most common question was, “How and when can I use it in the real world? And is that all Functional Programming is all about?” We’ll address all these questions here.

But before we go any further, we need to understand the basic pillars of FP and why is it worth learning FP in general.

What is Functional Programming?

Functional Programming (FP) is a programming paradigm or a way/style of programming which has the following characteristics:

  • We build programs by composing reusable “Pure functions”.
  • Write declarative code instead of imperative.

It’s more of a way of adapting your brain to think of the FP patterns in your programs.

We’ll learn about all of this using practical examples in a moment.

Why Functional Programming?

Below are the convincing reasons to learn FP:

Imperative vs Declarative code

Consider this code snippet which gets the best products out of an array of products.

Image for post

It does the job, it gives the correct output. But we had to read through the whole code like a story or a series of steps we’re telling the computer to execute in order to make meaning out of it. A computer is excellent at this job of executing programs step by step as a sequence of instructions. And above is a classical example of Procedural programming. So, even though the keyword “function” is used here, it doesn’t make it qualify for Functional Programming because it violates the fundamental principles of FP like pure functions and no side-effects.

Let’s see a declarative version of the above snippet.

javascript react haskell functional-programming curry function

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How native is React Native? | React Native vs Native App Development

Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.

Functional Programming in JavaScript

Let’s talk about the raw way of writing functions. Functions perform certain set of actions according to our requirements which could include fetching data, updating State, changing a set of mutable values and updating the DOM and so on.

Devmade Curry-ing Powder Recipe in Functional Programming

Ingredients: willingness to understand currying in fp, any language like JavaScript, Java, and a normal function with a fixed arity as…Definition: Currying is transforming a function by a fixed arity(number of given arguments) to a function that is a sequence of nested returned functions each takes one of those arguments at a time.

How to use Functional Programming in your React app | React Next

In the past few years, the buzz around functional programming has been growing, but it can be challenging to apply theoretical concepts to everyday work. How can we make the code we work on more functional? What advanced patterns can you use, and why should you do that? And what do React hooks have to do with all this? In this talk we’ll have a look at real-life examples and patterns you can use to make React apps more functional.

Forms of Composition in JavaScript and React

One of the core ideas in functional programming is composition: building larger things from smaller things. The canonical example of this idea should be familiar with legos.