6
function greaterThan(n) {
  return m => m > n;
}

I'm struggling to understand how this return statement works and what the 'm' variable actually does.

Falko
  • 995
  • 11
  • 21
  • It is a fat arrow syntax and the function returns an anonymous function to check if the m is greater. – Jai May 11 '18 at 05:42
  • @BhojendraNepal I will post on example of it. – Jai May 11 '18 at 05:45
  • 1
    this is [currying](https://www.sitepoint.com/currying-in-functional-javascript/) but since arrow syntax is used you may as well write it simpler: `const greaterThan = n => m => m > n;` – HMR May 11 '18 at 05:46
  • You can always [check it](http://babeljs.io/repl/#?babili=false&browsers=&build=&builtIns=false&code_lz=GYVwdgxgLglg9mABAcwE4FMCGV2oCoAWmYAFGAJSIDeAUIohlCKkgLaIC8AfIuz2AG4aAXxpA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&lineWrap=true&presets=es2015%2Creact%2Cstage-2&prettier=false&targets=&version=6.26.0&envVersion=). – Vucko May 11 '18 at 05:46

4 Answers4

4

greaterThan is the function which takes a parameter n and returns a function which takes parameter m. The returned function compares m and n and returns boolean value.

for Ex:

greaterThan(5)(4); // Returns false
Laxmikant Dange
  • 7,606
  • 6
  • 40
  • 65
2

The function greaterThan accepts a parameter n and returns a new function which accepts the parameter m which in turn returns true if the argument passed as m is greater than n;

Same method in ES5 code:

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

var greaterThan5 = greaterThan(5); // This returns a function

// Call that function with a parameter to check if that is greater than 5

console.log(greaterThan5(10)); //true [n=5, m=10]
console.log(greaterThan5(1)); //false [n=5, m=1]

//Note: The newly created function will have `n` "fixed" as `5` because of how JS closures work

Handy Reference:

Javascript Closures

Arrow Functions

JS Functions (and how they are first class objects in JS)

Chirag Ravindra
  • 4,760
  • 1
  • 24
  • 35
1

That is a fat arrow syntax. The snippet follows a closure feature of javascript, where you store one value and you pass another value in it to process.

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

console.log(greaterThan(5)(10));
console.log(greaterThan(5)(1));
Jai
  • 74,255
  • 12
  • 74
  • 103
0

What you see here is known as Currying.

Currying is the technique of translating the evaluation of a function that takes multiple arguments (or a tuple of arguments) into evaluating a sequence of functions, each with a single argument.

Currying is mainly a part of the functional paradigm. It is used mainly to construct functions that you can reuse later.

You can read more about currying in this SO question. As others have already mentioned, the greaterThan method returns a function which you can call in turn after you call greater than. The returned function returns a boolean based on the values you pass to it.

What the variable m actually does?

The function you have provided can be rewritten as:

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

So m is an argument to the function that is being returned. This function forms a closure on n and checks if m is greater than m and returns that value.

ayushgp
  • 4,891
  • 8
  • 40
  • 75