
One of the sweet things I really enjoy about JavaScript is the fact that it is a multi-paradigm programming language, you can
apply different paradigms of programming; object-oriented, functional, event-driven, etc
Although most times, we tend to treat JavaScript as a functional programming language based on our preference, one of the things
When dealing with JavaScript as a functional programming language, we need to note that we cannot be mutating states or change values assigned to variables.
A way we can achieve this is by using higher-order functions.
Before we begin to talk about higher-order functions in JavaScript let's understand some basic concepts about JavaScript that
will make reading this article easy to follow. Concepts like:
- declaring functions in JavaScript
- function arguments in JavaScript
- function return type
- functions in JavaScript as a first-class citizen
And later in this article, we will be touching:
-higher-order functions in JavaScript
-examples of higher-order functions in JavaScript
- use cases of high-order functions in solving problems in JavaScript
- writing your own higher-order functions in JavaScript
- Let's also play around with writing some built-in higher-order functions in JavaScript.
Let's dive in,
Declaring Functions in #"our first function")}//So when we want to make use of the function :firstFunction()
We just wrote a basic function, let's continue:
Let's write a function that can sum up two numbers; we will need two arguments, parameters are used when a function is declared but arguments are used when the function is called
// Definition:function sumTwoNumbers(num1,num2){ const sum = num1 + num2 return sum}//Let's call the function://Here we will pass the parameters which are in this case the two //numbers we want to add togetherconsole.log(sumTwoNumbers(3,7)// The result we have is 10
And the result will always be consistent and correct as long as nothing goes wrong in the function block
In the function we just wrote to sum up two numbers, we are actually returning the sum of the two numbers.. One thing we need to take note of is that
whenever we have a return statement in a function, the function stops executing at that point, we will make use of this later..
Functions in JavaScript can be treated as first-class citizens, what I am saying here is that functions can be treated as a variable in JavaScript, let me explain this
using the previous functions we have written.
//The first function we wrote can be written as :const firstFunction = ()=>{console.log("our first function")}
You notice we are declaring our function as a variable here, let's also re-write the second function
const sumTwoNumbers = (num1,num2) => num1 + num2
This looks shorter right? Let me explain what is happening here, instead of writing long lines of code, if you want to write a function that does not require
multiple lines of logic, you also notice we don't explicitly write a return statement but as far as there is no curly brace after the big fat arrow, the function
returns the value of _num1 + num2
_
We can now safely move on to higher-order functions in #"http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24">