Javascript : Higher Order Functions
This is my another article on a javascript topic. This article is really posted on my personal blog at http://www.vipincheriyanveetil.com/blog/javascript-higher-order-functions/
Hello to all those wonderful programmers out there 🙂
This post is to make us understand
- What is Higher Order Functions ?
- Example and its usage.
This topic was confusing and complicated for me when I first heard about this during an interview that I was attending. I had no clue what this is and I just said “i don’t know and I never used this“. Like every other programmers i was also curious and wanted to go see what the hell is “higher order functions“. Oh my god, to my surprise , I found that this is something which everybody is already using while writing programs in javascript. I then thought I should write this as a post in my blog at least for my reference and will help me to recollect this if I face the same question during my future interviews. So this is that post.
Whats “Higher Order Functions” ?
The truth is that “Higher Order Functions” are extensively used in javascript and every programmer uses it without even knowing it. Other reality is that it is this concept that makes javascript suitable for functional programming. So a simple definition can be that Higher Order Functions are functions that can accept or return another function . I am now wondering whats Callback function then ??
Callback and Higher Order Function
Let’s see an example straight away. This is the same example which is used in one of my other article
function announce() {
console.log('Yea!, the hot water is ready !')
}function boil_water(shoutIt) {
console.log('Water is boiling')
setTimeout(() => {
shoutIt()
}, 3000)
}boil_water(announce)
As per the above example
“boil_water” is a “Higher Order Function” because it takes “announce” function as its input.
“announce” function is a Callback function because it’s getting inserted to the higher order function for a purpose.
Ok, I am now getting the difference between them. So they both help each other to become what they are.
Please refer here on my article about callbacks and more info.
What is Functional Programming ?
Now this should be easy for me to define and understand. So, functional programming is a form or type of programming where functions can be passed to another function as parameter and functions can be returned from another function as return values.
First Class Citizens
Functions are treated as ‘First Class Citizens”. More definition of the same can be found at https://en.wikipedia.org/wiki/First-class_citizen
Get Vipin Cheriyanveetil’s stories in your inbox
Join Medium for free to get updates from this writer.
This also means that Functions can be :
- Used as arguments to other functions.
function just_shout(action) {
return action();
}just_shout(function () { return "hello world" })
// Returns "hello world"
- Returned from another function.
function just_shout() {
return function () { return "hello world" };
}var res = just_shout();
res();
// Returns "hello world"
- Assigned to variables.
var just_shout = function() {
return return "hello world" ;
}just_shout();
// Returns "hello world"
Built-in Higher Order Functions.
There are few javascript Built-in Higher Order Functions. I am sure we all have used them without knowing that they are Higher Order Functions. Some of them which are mostly used and familiar are below.
- Array.prototype.filter. (this is my favourite)
var marksList = [10, 67, 53, 39, 85]
function isPassed(value) {
return value > 45;
}
var passedMarks = marksList.filter(isPassed);
console.log(passedMarks)
// Prints
[67, 53, 85]- Array.prototype.map
var numbersList = [3, 5, 7, 9, 11]
var multipliedBythree = numbersList.map(function(num) {
return num * 3;
});
console.log(multipliedBythree)
// Prints
[9, 15, 21, 27, 33]Conclusion
I hope this post was useful and informative. So it’s just simple that, Higher Order Functions are functions that can accept or return another function. We also saw few example above . We then understood a bit about functional programming. Finally we also listed out few built-in higher order functions which are popularly used when programmers put their hands in to javascript programming.
Thanks for spending some time with this post. Have a great day and do enjoy programming..
You can buy me a coffee , if you like at https://www.buymeacoffee.com/vipinc007









