I don’t understand the logic of this code: in fact I’m writing the console.log once, why does it display the text in front of it: I want to eat apple, how does it work explain? I do not call the function, but simply assign it to a variable a.

function foodDemand (food) { console.log("I want to eat" + " " + food); } var a = foodDemand("apple"); console.log("apple"); // -> I want to eat apple // -> apple 

    4 answers 4

    Let's take it in order. In JavaScript, a function is a first-class object . You can perform various actions with it.

    Can save function to variable

     var f = function() { // ... }; 

    You can pass a function to a function as an argument:

     function runner(task) { task('foo bar baz'); } 

    You can return the function from the function as a result:

     function builder() { return function() { // ... } } 

    You can create a function at runtime:

     var f; if (Math.random() > 0.5) { f = function() { console.log('low'); }; } else { f = function() { console.log('high'); }; } 

    And, unlike other objects of the first class, the function can be called. To do this, there is a special syntax () (parentheses):

     var f = function() {/* ... */} // Вот так функция вызывается f(); 

    Now back to your question. Right here:

     function foodDemand (food) { console.log("I want to eat" + " " + food); } 

    You declare a function that is accessible by the foodDemand .

    Speaking

    I do not call the function, but simply assign it to a variable a.

    You're wrong. You just call the function and assign the result of its execution to the variable a .

    The assignment of the function to the variable a must be of the form:

     // Обратите внимание на отсутствие круглых скобок. var a = foodDemand; 
    • Perhaps the TC wanted to get a variable in the closure that calls foodDemand with the argument "Apple" ? - VladD
    • No closure I did not want to get. I am having trouble understanding this expression: var a = foodDemand ("apple"); - I called the function it was executed and I wrote the result in a. It seems after clarification it became clear to me. thanks everyone! - spectre_it

    Just in this code, you call the foodDemand function. In this case, since the function does not return anything, the value of the variable a == undefined , as can be easily seen by calling console.log(a) .

    If you need a variable а be a function, then you should write it this way (without parameters):

     var a = foodDemand; a("apple"); // I want to eat apple 

      In contrast, the function you are calling. And when she calls, she writes to the console.

      To assign a function a foodDemand use the following code

       function foodDemand (food) { console.log("I want to eat" + " " + food); } var a = foodDemand; console.log("apple"); 

      and where required call

       a('apple'); 

      do not forget that the local variable a will be available only inside the module where you declared it. to make it available globally, you can not use var in the declaration and then the variable will be available on the page as a property of the window object

        What are you kid uploaded))

        1. You call the foodDemand function, which writes text to the console (by condition)

        2. You call console.log, which writes text to the console

        • For me, this is quite a normal explanation. I come here and come to understand the essence of the problem, experienced comrades help to do this. Explaining everything in order from the very beginning. This is the idea of ​​the site. What to bring people to correct reflections. That that I cause function, and she writes down in the console I and so perfectly see. But this is why it could not understand. Not asked yet) - spectre_it