What is Functional Programming? A Beginner's JavaScript Guide

What is Functional Programming? A Beginner's JavaScript Guide

Functional Programming in JavaScript teaches JavaScript developers functional techniques that will improve extensibility, modularity, reusability, testability, and performance. Functional programming is a sub-paradigm of the Declarative programming paradigm, with its own rules to follow when writing code. JavaScript is a multi-paradigm language and can be written following different programming paradigms. A programming paradigm is essentially a bunch of rules that you follow when writing code.

JavaScript is a multi-paradigm language and can be written following different programming paradigms. A programming paradigm is essentially a bunch of rules that you follow when writing code.

These paradigms exist because they solve problems that programmers face, and they have their own rules and instructions to help you write better code.

Each paradigm helps you solve a specific problem. So it's helpful to have an overview of each of them. We'll cover functional programming here.

At the end of this article, there are some resources you can use to go further if you enjoyed this introduction.

There's also a GitHub glossary that'll help you decode some of the jargon that functional programming uses.

Lastly, you'll find a place to get your hands dirty coding with practical examples and a GitHub repo full of resources you can use to learn more. So let's dive in.

Declarative vs Imperative Programming Paradigms

One example of these paradigms I talked about at the beginning is object-orientated programming. Another is functional programming.

So what exactly is functional programming?

Functional programming is a sub-paradigm of the Declarative programming paradigm, with its own rules to follow when writing code.

What is the declarative programming paradigm?

If you're coding in a language that follows the declarative paradigm, you write code that specifies what you want to do, without saying how.

A super simple example of this is either SQL or HTML:

SELECT * FROM customers
<div></div>

In the above code examples, you aren't implementing the SELECT or how to render a div. You are just telling the computer what to do, without the how.

From this paradigm, there are sub-paradigms such as Functional programming. More on that below.

What is the imperative programming paradigm?

If you're coding in a language that follows the imperative/procedural paradigm, you write code that tells how to do something.

For example, if you do something like below:

for (let i = 0; i < arr.length; i++) {
     increment += arr[i];
}

You are telling the computer exactly what to do. Iterate through the array called arr, and then increment each of the items in the array.

Declarative vs Imperative programming

You can write JavaScript in the Declarative paradigm or the Imperative paradigm. This is what people mean when they say it's a multi-paradigm language. It's just that functional code follows the Declarative paradigm.

If it helps you remember, an example of a declarative command would be to ask the computer to make you a cup of tea (I don't care how you do it, just bring me some tea).

Whilst imperatively, you would have to say:

  • Go to the kitchen.
  • If there is a kettle in the room, and it has enough water for a cup of tea, turn on the kettle.
  • If there is a kettle in the room, and it doesn't have enough water for a cup of tea, fill the kettle with enough water for a cup of tea, then turn on the kettle.
  • And so on

So what is Functional Programming?

So what does this mean for functional code?

Because it's a sub-paradigm from the Declarative paradigm, this affects the way you write functional code. It generally leads to less code, because JavaScript already has a lot of the in-built functions you commonly need. This is one reason people like functional code.

It also allows you to abstract away a lot (you don't have to understand in depth how something gets done), you just call a function that does it for you.

And what are the rules that lead to functional code?

Functional programming can be simply explained by following these 2 laws in your code:

  1. You architect your software out of pure, isolated functions
  2. You avoid mutability and side-effects

Let's dig into that.

javascript functional programming developer

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

Functional Programming in JavaScript Explained

Functional programming has become a really hot topic in the JavaScript world. You'll learn: what functional programming is, the types of functions there are, the principles of functional programming, and have a deeper understanding of Higher Order functions.

filterMap in JavaScript, efficiency for functional programming

All JavaScript developers are awesome and they deserve a brilliant way of filtering and mapping over an array with just one function with a linear complexity O(n) of one run through the array.

Understanding Functional Programming in JavaScript

Functional Programming in JavaScript teaches you techniques to improve your web applications: their extensibility, modularity, reusability, and testability, ...

Functional JavaScript :  Functional Programming and Arrays

Functional JavaScript :  Functional Programming and Arrays. Create our own map and filter functions. We’ll look at how to create our own array methods. JavaScript is partly a functional language. To learn JavaScript, we got to learn the functional parts of JavaScript.

What is Functional Programming?

Most of what I will discuss in this article is knowledge accumulated from reading, “Functional Programming in JavaScript”, by Luis Atencio. In simple terms, functional programming is a software development style that places a major emphasis on the use of functions.