I have two questions regarding the following code sample:
function greaterThan(n) {
return m => m > n;
}
let greaterThan10 = greaterThan(10);
console.log(greaterThan10(11));
//returns true
From what I understand the function greaterThan(n) returns a function which takes the parameter m but here it starts to get tricky for me:
m not declared and still properly working within this code sample?greaterThan10 is declared as a variable in line 4 and it is inheriting the value of greaterThan(10). How can it behave like a function in line 5, where this "variable" takes another integer?greaterThan is a higher order function; it returns another function that it creates based on the inputs.
m => m > n is an arrow function.
It's equivalent to this:
function greaterThan(n) {
return function (m) {
return m > n;
}
}
let greaterThan10 = greaterThan(10);
console.log(greaterThan10(11));
//returns true