Javascript function in Eloquent Javascript

higher-order functions javascript w3schools
higher order functions javascript medium
return function javascript
javascript functions
javascript eloquent javascript
eloquent javascript chapters
eloquent javascript chapter 4
eloquent javascript projects

How does greaterThanTen(9) become the y variable in the return function? What I mean is how does the parameter (9) become the y in the return function argument? Wouldn't 9 be replaced with x since greaterThanTen = greaterThan(10)? Wouldn't the 9 just replace the x = 10 parameter? I just don't understand how that 9 parameter is getting to y in the return function.

 function greaterThan(x) {
   return function(y) {
     return y > x;
   };
 }

 var greaterThanTen = greaterThan(10);
 show(greaterThanTen(9));

This is what functions were invented for: They are canned code that a program can go through whenever it wants. Putting a string on the screen requires quite a​  function greaterThan10(y) { return y > 10; } Functions that create functions take advantage of closures ("the thing that 'captures' the 10). The advantage is that you don't need to keep writing greaterThanNnn functions for every number you want to use, you just call greaterThan(n) for whatever you need. This is a contrived, but popular, example, of course.

when you called greaterThan(10) it assigned value of x and return the function in greaterThanTen variable which now becomes

var greaterThanTen = function(x){10>x};

then in next line you called greaterThanTen(9) so it will assign the x value. i hope you understood what i said.

You've seen function values, such as alert , and how to call them. Functions are the bread and butter of JavaScript programming. The concept of wrapping a  Once you call the function greaterThan it creates another function. But as you pass the first argument 10, it takes the place of x . Further when you call the function and pass the second argument 9 it takes place on y the function built inside.

You must have to know the basis about the closures concept in JavaScript. Closures are the most tricky and special addition to the JavaScript language. As you will notice they follow the lexical scope of the language flow. Here in this example;

 function greaterThan(x) {
   return function(y) {
     return y > x;
   };
 }

 var greaterThanTen = greaterThan(10);
 console.log(greaterThanTen(9));

If you see there is the main concept of closures. Once you call the function greaterThan it creates another function. But as you pass the first argument 10, it takes the place of x. Further when you call the function and pass the second argument 9 it takes place on y the function built inside. In this way the values are stored in the function call stack and you can compare and operate on those values.

Since “doing something” can be represented as a function and functions are just values, we can pass our action as a function value. function repeat(n, action) { for (  JavaScript provides a way to make defining this type of function easier. If you put the keyword new in front of a function call, the function is treated as a constructor. This means that an object with the right prototype is automatically created, bound to this in the function, and returned at the end of the function.

Different functions might need a different number or different types of arguments. The prompt function isn't used much in modern web programming, mostly  Being able to pass function values to other functions is a deeply useful aspect of JavaScript. It allows us to write functions that model computations with “gaps” in them. The code that calls these functions can fill in the gaps by providing function values. Arrays provide a number of useful higher-order methods.

Since 'doing something' can be represented as a function, and functions are also values, we can pass our action as a function value: function forEach(array, action​)  to describe the way the JavaScript language should work so that the various pieces of software that claimed to support JavaScript were actually talking aboutthesamelanguage.

The third edition of Eloquent JavaScript was made possible by 325 financial The console.log function . Functions and side effects . You can use JavaScript functions to create local scopes and objects to represent module interfaces. This is a module for going between day names and numbers (as returned by Date ’s getDay method).

Comments
  • Check out this answer stackoverflow.com/a/20446108/2415293
  • This is a great example, I now have a deeper understanding of closures.
  • Ok, your answer is really helping me understand this now. I think my problem is I'm not understanding what was happening in that return function.
  • @Vodka_Tonic It was returning ;) In JS, functions themselves can be values, like 42 or "ohai". This is nice.