First Class Functions, Higher Order Functions & Callback Functions

First Class Functions

var add = (x, y) => x + y;
var subtract = (x, y) => x - y;
var multiply = (x, y) => x * y;
function numbers(fn, x, y){
return fn(x, y);
}
var addResult = numbers(add, 3, 2);
var subtractResult = numbers(subtract, 3, 2);
var multiplyResult = numbers(multiply, 3, 2);

Higher Order Functions

var nums = [1, 2, 3, 4, 5];var numsTimesTwo = nums.map(num => num * 2);// numsTimesTwo = [2, 4, 6, 8, 10];
var nums = [1, 2, 3, 4, 5];var multiplyByTwo = num => num * 2;var numsTimesTwo = nums.map(multiplyByTwo);// nums.map(multiplyByTwo) 
// would be exactly the same as
// nums.map(num => num * 2)
var nums = [1, 2, 3, 4, 5];var oddNumbers = nums.filter(num => num % 2 !== 0 );// oddNumbers = [1, 3, 5];
var nums = [1, 2, 3, 4, 5];var isOdd = num => num % 2 !== 0;var oddNumbers = nums.filter(isOdd);

Functional Programming Paradigm

var nums = [1, 2, 3, 4, 5];if( nums.every(isOdd) ){
console.log('Yes, they are all odd!');
} else {
console.log('Nope, they are not all odd!');
}
var nums = [1, 2, 3, 4, 5];if( nums.some(isOdd) ){
console.log('At least one of these numbers are odd!');
} else {
console.log('None of these numbers are odd!');
}

--

--

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