Principles of Functional Programming in JavaScript That Will Make Your Coding Life Easier

Principles of Functional Programming in JavaScript That Will Make Your Coding Life Easier

Who else loves to write side-effects-free functions? I think we, as programmers, all do. Today, in this story, I will walk you through the basic principles of functional programming that will make your coding life easier.

Who else loves to write side-effects-free functions?

I think we, as programmers, all do.

Today, in this story, I will walk you through the basic principles of functional programming that will make your coding life easier.

Let’s get into it.

1. Pure Functions

When it comes to pure functions, you need to remember only two things:

  • They return the same result if given the same arguments.
  • They don’t cause any side effect.

Returning the same result if given the same arguments

Let’s look at the simple example below:

const DISCOUNT = 0.5;

const calculatePrice = price => price * DISCOUNT;
let actualPrice = calculatePrice(15); // 7.5

Is the function *calculatePrice *pure? No, it’s not.

Why? Because the global variable DISCOUNT is not passed to the function as an argument. If you change the DISCOUNT’s value, the returning result will also change, although the argument price stays the same.

To make it pure function, you have to add one more parameter to the function like this:

const DISCOUNT = 0.5;

const calculatePrice = (price, discount) => price * discount;
let actualPrice = calculatePrice(15, DISCOUNT); // 7.5

javascript-tips javascript-development coding javascript functional-programming

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

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript. Bugs are born in many ways. Creating side effects is one of them. Some people say side effects are evil, some say they’re not.

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.

How to start writing less error-prone code in JavaScript

How to start writing less error-prone code in JavaScript - Everything in JavaScript is an object!’. We said that this assertion is false. Many things in JavaScript can behave like an object, but that doesn’t mean it’s the object. We can say we have three types of objects (objects, functions and arrays) in JavaScript.

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.

13 Useful JavaScript Developer Tools for Writing High-Quality Code

Today I’m going to show you 13 well-know and popular tools to write better and cleaner JavaScript code. These are tools that I’m using right now on my JavaScript developer journey. So if you want to improve the quality of your projects, this list is for you.