function - javascript -why variable statement is executed? -


i in beginning of learning javascript , trying understand of it's logic. read variable box ,a way of storing , keeping track of information in program.

so why code been executed?

    var person = prompt("please enter name"); 

if variable box suppose contain prompt function.then how , why function invoked ,shouldnt call function invoke it? like

person(); 

and if function assigned variable self-invoked-like this??

     var person = myfunc() { /*--code here--*/ }; 

because aren't passing prompt function, you're calling prompt function , storing result.

another example:

function add(a, b) { return + b; } var fiveplusthree = add(5, 3); // fiveplusthree === 8 var addfunction = add; //addfunction === add, passed function itself. var oneplusone = addfunction(1,1); // oneplusone === 2 

you can pass function expressions variables, example person that. however, result of calling function has kept elsewhere

var promptperson = function() { return prompt("please enter name"); } var name = promptperson(); 

however, it's not different calling prompt directly first example.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -