First Class Functions and High Order Functions in JavaScript

A function is a block of code designed to perform a particular task. Functions are very important and powerful fundamental building blocks in JavaScript. If you are starting to get involved with functions in JavaScript, you’ve probably started to hear terms “First Class Functions” and “High Order Functions”. Let’s take a look at what these terms are trying to convey, the connection between them, and what they provide us.

const summation = function (a,b) {
return a+b
}
summation(1,2) // 3
const foo = {
value: 1,
summation: function(a,b) {
return a+b+this.value
}
}
const sum = foo.summation(2,3)
console.log(sum) // 6
function sayHello() {
return "Hello ";
}
function sayHelloWorld(sayHello, param) {
console.log(sayHello() + param);
}

sayHelloWorld(sayHello, "World!") // "Hello World"
function sayHelloWorld() {
return () => {
console.log("Hello World!");
};
}

// First Approach
sayHelloWorld()() // "Hello World!"

// Second Approach
const greeting = sayHelloWorld()
greeting() // "Hello World!"
const fibonacci = (n) => (n <= 2 ? 1 : fibonacci(n - 1) + fibonacci(n - 2));

console.log(fibonacci(10)) // 55
function sayHelloWorld() {
console.log(`${this.firstWord} ${this.secondWord}`);
}

const obj = {
firstWord: "Hello",
secondWord: "World!",
};

sayHelloWorld.call(obj) // "Hello World!"
const arr = [1, 2, 3, 4];

const initialValue = 0;
const sumWithInitial = arr.reduce(
(accumulator, currentValue) => accumulator + currentValue,
initialValue
);

console.log(sumWithInitial) // 10
const sayHelloWorld = () => console.log("Hello World")
btn.addEventListener("onclick" , sayHelloWorld)

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store