ES6 & The Functional Way of Life
Introduction
So, if you’ve been playing with JavaScript for some time, you’ve without a doubt heard of the term ‘ES6’, there’s a good chance you may even know what ES6 means. To keep things light & to only ease the newcomers into ES6, it’s essentially an implementation of ECMAScript, what this means in English is that it’s added features & some form of functionality to the JavaScript language. A simple example is how we now have constants in the JavaScript ecosystem, which prior to ES6, such thing didn’t formally exist.
Now, if you’d like to learn more about what exactly the
ECMAScript is, I recommend you checkout sources such as this. If you want a long story cut short, you can think of ECMAScript as a scripting language specification. It’s not purely limited to just JavaScript, when flash was a thing, ActionScript was also following the ECMAScript specification(s).
For those of you that don’t care too much about the nitty-gritty side of things, in addition to allowing us to implement new & cool features, it also means that we are now able to make use of new & alternative syntax(es). An example being what’s commonly referred to as an ‘arrow_function‘,
in basic terms this is where you no longer need to use the word ‘function‘ to define a function. Instead, if you’re familiar with lambda functions then you’re probably already familiar with the syntax, in very basic terms it’s just a much shorter & in my opinion easier way to implement a function.
Okay, so we know about ES6, but what is ‘Functional Programming‘?
This is a good question & if you’ve asked yourself this question, I’m glad to be the one to introduce you to the programming paradigm known as functional programming. In a nutshell, functional programming is a more
declarative way to write your code. Furthermore, functional programming requires you to follow a certain set of rules or principles.
There’s many a debate as to what these principles are & how religiously they must be followed in order for some code to be classified as an implementation of functional programming. My personal guideline is that you make use of pure functions, currying, immutability, etc, however, I don’t like to be too strict with these guidelines.
In some languages they’re purely functional, however, JavaScript is not one of those languages, JavaScript is quite an interesting language for this as it supports quite a number of different paradigms. Of course, I’d assume that most of you whom may be reading this post would already be aware of OOP, well JavaScript code is commonly written in an OOP-like fashion. I’ve heard many developers refer to functional programming in JavaScript as ‘a functional-style implementation‘.
You now may be wondering as to why I would say how I don’t like to be too strict, the short & simple answer is that I like to implement my solutions in the cleanest, most concise & simplistic ways possible. Personally I’m a religious believer in the KISS programming principle(s), if you can make it more simplistic, why not?
But back to the point, with JavaScript, essentially, in order for you to follow some of these functional concepts, you need to first understand what some of the terms I’ve mentioned means. So, to save you having to carryout research to gain a basic, here’s list of short & simple definitions.
- Immutability – Preventing data from changing, or in a more formal phrase ‘data whose state cannot be modified after it’s been created‘.
- Currying – A function that takes one argument & returns another function that takes one argument.
- Pure Functions – A function that will always return the same value for the same arguments, i.e. a function that adds 2 to some integer, furthermore the function has no side effects.
- First Class Functions – Essentially you may pass functions as arguments to other functions.
Example
Within the example I’ve provided, the code will simply take some dictionary, aka an array of strings, and some word, aka a single string, then find all anagrams of the provided word within the dictionary.