0

I found the following example in a course and I don't understand it.

This code takes two arguments, in this case two years, and returns true if 1912 is within those two years, otherwise the result is false. Example:

function yearToCheck(year) {
  return function(x, y) {
    return year >= x && year <= y;
  }
}

const checkYears = yearToCheck(1912);

checkYears(1900, 1913) // true 
checkYears(1900, 1911) // false

The code works. I just want ot undestand why does the anonymous function "function (x, y)" take the values 1900 and 1913/1911 from the variable checkYears?

ThS
  • 4,597
  • 2
  • 15
  • 27
  • Not much of a question here. You call a function (`yearToCheck`), the return value (assigned to `checkYears`) is a function that takes 2 parameters. Call it with parameters and get the result of the logical condition contained within that function (i.e. a boolean). The code here is very readable. – Adam Jenkins Nov 06 '22 at 16:14
  • [Documentation on closures](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures). – Andy Nov 06 '22 at 16:28

0 Answers0