Understanding return function(x) from outer function(array)
<p>I am learning JavaScript and got a bit confused with the following exercise. I did had to create a filter that would accept another functions as a sorting method. The thing that I cant understand a bit is how exactly the <code>for loop</code> passes the <code>value</code> to the <code>x</code>. Can you please explain?</p>

<pre><code>function filter(arr, func) {

    var result = [];

    for (var i = 0; i &lt; arr.length; i++) {

        var value = arr[i];
        if (func(value)) {
            result.push(value);
        }
    }

    return result

}

function inBetween(a, b) {
    return function(x) {
        return a &lt;= x &amp;&amp; x &lt;= b;
    }
}

function inArray(arr) {
    return function(x) {
        console.log(x)
        return arr.indexOf(x) != -1;
    }
}

var arr = [1, 2, 3, 4, 5, 6, 7];

alert(filter(arr, function(a) {
  return a % 2 == 0
})); // 2,4,6

alert( filter(arr, inBetween(3, 6)) ); // 3,4,5,6

alert( filter(arr, inArray([1, 2, 10])) ); // 1,2
</code></pre>