Functional JS #1: Introduction

Krzysztof Czernek
DailyJS
Published in
5 min readSep 28, 2018
“written equations on brown wooden board” by Roman Mager on Unsplash

Introduction

If you are a JavaScript developer, there’s a good chance you’ve come across concepts like “functional programming”, “functors”, “closures”, or “immutability”. You might be wondering what the heck all these things mean. Maybe you’ve already done some more reading. Or maybe you’re trying to incorporate some of the FP practices into your code.

If that’s the case, then I guess we’re in the same boat!

I started using JavaScript professionally some 2 years ago. Since then, I’ve been trying to deepen my knowledge of functional programming.

In the beginning, I was baffled by things like “currying”, or my colleague telling me to “use map instead of forEach and avoid these side effects”.

I’ve learned a lot since then. Trying to understand FP practices has been a really stimulating experience — and still is!

Now, I want to distill what I’ve learned so far into a series of articles — a series I wish I’d read at the beginning of my journey. I am hoping this will prove useful for people with a background like the one I had a couple years ago.

What is Functional Programming?

Functional programming is a programming paradigm — a way of thinking about, and structuring your code. It’s an alternative to procedural or object-oriented programming.

FP aficionados would say they aim to optimize for code reusability, readability, and testability. The tools in their toolboxes are functions, ways of composing them to model complex behavior, and avoiding shared state and side effects.

If these words mean nothing to you — don’t worry, we’ll get there!

Why should you learn Functional Programming?

There’s a ton of articles out there on the Internet that go on about what makes Functional Programming better than Objective Programming. About how it produces more readable code that is easier to extend, test, and maintain; is more declarative, and so on.

And I agree with most of those. I don’t want to repeat the same arguments over again, so I would like you to take a look at a few of the links above.

What I will tell you, though, is something else. I will tell you why you should consider learning Functional Programming. I’ll share what was in it for me, and what might be in it for you as well.

It is becoming mainstream

If you look at the ever-changing JS ecosystem, the increasing adoption of functional programming practices is clear. A lot of the cool new things are heavily influenced by FP:

  • React — with its reusable components and pure functions
  • Redux — and the way it forces you to avoid mutating state
  • Reason — and its functional approach and immutability
  • Elm — being a functional language that compiles to JavaScript
  • Underscore.js, lodash, and Ramda — all having FP utility functions baked in

There’s a good chance you already are or will be using one of these tools. If you understand the concepts behind Functional Programming well, you will master these frameworks and libraries faster. If you don’t, well, you’re going to be running behind.

It is challenging

Have you ever thought that the last time your brain was working at full speed was, like, in college?

I certainly have. I mean, my job is challenging. I spend a lot of time thinking hard: about the business problem, architecture of the system, team dynamics, and lots of other stuff.

It’s just not the feeling I got in, say, calculus classes. There, I needed to twist my brain to understand all the concepts, and how they play with each other.

Well, trying to learn functional programming is kind of like that. Not without reason — functional programming is deeply rooted in maths.

There will be a lot of new terms, and trying to digest all this knowledge might make your head spin. But you’re in for a pleasant surprise — as soon as these things click, you will feel huge satisfaction.

I believe that if you enjoyed maths in college, you will enjoy learning this stuff as well.

Now that I think about it… it’s probably not the best way to get you hooked on functional programming, is it?

It’s something new

I believe that learning a new programming paradigm is beneficial even if you don’t plan to switch to it in your day-to-day work. Not only because it’s challenging — but also because it feels different.

Trying a new style of coding will expose you to alternative ways of doing things. It will force you to think about problems from a different point of view.

You might think that your time is better spent mastering the tools you’ve been using so far. I know it feels more rewarding, pragmatic, and productive. It deepens your tech expertise.

I believe that we, as software developers, also need to broaden our experience. It helps us communicate with other developers better, especially if they have different backgrounds. It helps us make better decisions — by approaching problems from new directions.

Learning a new programming language is a great way to broaden your expertise. Learning a new programming paradigm is even better.

OK, but… why JavaScript?

If you’ve decided you want to learn FP, the next question is: is JavaScript a good way to do it?

Let me start by saying that JavaScript is probably not the best way to learn Functional Programming concepts.

Other, more functional languages, have immutable data structured built in. JavaScript doesn’t, you need to use a library for that. Using one, however, makes your code more verbose at the points where the library’s code meets yours.

JavaScript tries to look like Java, with its class, new , and all this stuff.

The JavaScript standard library kind of sucks. It certainly was not designed with FP concepts in mind.

The point here is: there are a lot of arguments to be made about JS not being the best choice for Functional Programming.

On the other hand, JavaScript has a lot of things going for it, that make functional programming easier:

  • First-class functions
  • Closures
  • Object and array literals
  • Plenty of FP libraries

Also, and I think this is the most important argument: JavaScript is very popular, and is getting more popular by the day.

If you are going to use JavaScript anyway, why not try to learn the new paradigm where it can actually be useful?

Let’s get started!

In the next parts of this series, I will introduce some FP concepts — starting from the very basic ones. I’m planning to cover the following aspects:

  • Pure functions, First-class functions, Higher-order functions
  • Shared state, Immutability, Side effects, Referential transparency
  • Closures
  • Partial application, currying
  • Recursion
  • Composition, point-free style
  • FP libraries
  • Functors, Applicative Functors, Monads

The assumption we will be working with is that we all know JavaScript basics, ES6, and so on. It is not a hard requirement in order to understand FP concepts. It will, however, make it easier to work through examples and trying things on your own.

Get ready, and I’ll see you in the next part: “Functional JS #2: Functions. Duh!”.

--

--