0

I have a function that takes an input (n) and puts it in the parameters of another function. (n) represents in the second equation the number to what (m) is being compared to (in this case 10). I understand how this function is structured, just don't understand what this means:

return m => m > n;


function greaterThan(n) {
  return m => m > n;
}
let greaterThan10 = greaterThan(10);
console.log(greaterThan10(9)); //should output false
imjared
  • 15,060
  • 2
  • 42
  • 66
awoldt
  • 171
  • 1
  • 4

3 Answers3

2

m => m > n is an arrow function in javascript. almost same as,

function(m){
  return m>n
} 

Read more here http://2ality.com/2012/04/arrow-functions.html

Shishir Arora
  • 4,257
  • 2
  • 22
  • 30
2

This is an example of currying. The function greaterThan is returning a new anonymous function. It's probably easier to understand written like this:

function greaterThan(n) {
  return function (m) {
    return m > n
  }
}

Calling greaterThan(10) returns a new function that will compare its argument to 10. In your example you give it the name greaterThan10 so now you can call greaterThan10(9). This is all because the other function returns another function to use.

ajthyng
  • 1,061
  • 1
  • 13
  • 17
0

You can rewrite your example:

function greaterThan(n) {
  return function(m) { return m > n }
}

which uses the same function syntax for both of them. Otherwise I found your explanation to be let much what anyone would write to explain it. m is simply the parameter to the function returned.

Joey
  • 316,376
  • 76
  • 642
  • 652