The functional programming paradigm
When writing functional programming (FP) code, we keep data and functionality separate and pass data into functions only when we want something computed.
In functional programming, functions return new values and then use those values somewhere else in the code.
function getDistance(mph, h) {
return mph * h
}
var mph = 60;
var h = 2;
var distance = getDistance(mph, h);
console.log(distance);
Another style is object-oriented programming (OOP). In this style, we group data and functionality as properties and methods inside objects.
For example, if I have a virtualPet object, I can give it a sleepy property and a nap() method:
var virtualPet = {
sleepy: true,
nap: function() {}
}
In OOP, methods update properties stored in the object instead of generating new return values.
For example, if I check the sleepy property on the virtualPet object, I can confirm that it’s set to true.
However, once I’ve ran the nap() method on the virtualPet object, will the sleepy property’s value change?
//creating an object
var virtualPet = {
sleepy: true,
nap: function() {
this.sleepy = false
}
}
console.log(virtualPet.sleepy) // true
virtualPet.nap()
console.log(virtualPet.sleepy) // false
OOP helps us model real-life objects. It works best when the grouping of properties and data in an object makes logical sense - meaning, the properties and methods “belong together”.
There are many more concepts and ideas in functional programming.
Here are some of the most important ones:
- First-class functions
- Higher-order function
- Pure functions and side-effects
First-class functions
It is often said that functions in JavaScript are “first-class citizens”. What does that mean?
It means that a function in JavaScript is just another value that we can:
- pass to other functions
- save in a variable
- return from other functions
In other words, a function in JavaScript is just a value - from this vantage point, almost no different then a string or a number.
function addTwoNums(a, b) {
console.log(a + b)
}
function randomNum() {
return Math.floor((Math.random() * 10) + 1);
}
function specificNum() {
return 42
};
var useRandom = true;
var getNumber;
if(useRandom) {
getNumber = randomNum
} else {
getNumber = specificNum
}
addTwoNums(getNumber(), getNumber())
Higher-order functions
A higher-order function is a function that has either one or both of the following characteristics:
- It accepts other functions as arguments
- It returns functions when invoked
There’s no “special way” of defining higher-order functions in JavaScript. It is simply a feature of the language. The language itself allows me to pass a function to another function, or to return a function from another function.
Pure functions and side-effects
Another concept of functional programming are pure functions.
A pure function returns the exact same result as long as it’s given the same values.