tali
tali

Reputation: 3

javascript -why variable statement is executed?

I am in the beginning of learning javascript and trying to understand some of it's logic. I read that a variable is like a box ,a way of storing and keeping track of information in a program.

so why is this code been executed?

    var person = prompt("Please enter your name");

if variable is a box that suppose only to contain the prompt function.then how and why this function is invoked ,shouldnt you call a function to invoke it? like

person();

and if so then any function assigned to a variable will be self-invoked-like this??

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

Upvotes: 0

Views: 66

Answers (5)

Madara's Ghost
Madara's Ghost

Reputation: 174957

Because you aren't passing the prompt function, you're calling the prompt function and storing the result.

Another example:

function add(a, b) { return a + b; }
var fivePlusThree = add(5, 3); // fivePlusThree === 8
var addFunction = add; //addFunction === add, I passed the function itself.
var onePlusOne = addFunction(1,1); // onePlusOne === 2

You can pass function expressions to variables, your example with the person is exactly like that. However, the result of calling the function has to be kept elsewhere

var promptPerson = function() { return prompt("Please enter your name"); }
var name = promptPerson();

However, it's not much different from just calling prompt directly like your first example.

Upvotes: 3

Quentin
Quentin

Reputation: 943537

The right hand side of the = is evaluated and the result of that evaluation is assigned to the variable on the left hand side.

Because prompt has () after it (it has some arguments inside too), evaluating it calls the function and it is the return value that gets assigned to the variable.

To assign the function to the variable you would just have:

var person = prompt;

If you wanted to store the arguments, then you would need to create a new function.

var person = function () {
    prompt("Please enter your name");
};

or

var person = prompt.bind(this, "Please enter your name");

and if so then any function assigned to a variable will be self-invoked-like this??

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

No function is involved in that code.

The right hand side is a named function expression. There is no function call involved. The result of evaluating it is a function. That function is assigned to person.

For it to be self-invoked you would have to call it.

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

… in which case the return value would be assigned to person.

Upvotes: 0

Kell
Kell

Reputation: 3317

The function is invoked because the syntax you've written means "set the value of person the the result of the prompt function". If you want to create a function pointer you do this:

var person = function(){ prompt("Please enter your name");}

Upvotes: 0

Radonirina Maminiaina
Radonirina Maminiaina

Reputation: 7004

You execute the prompt() javascript function that will return something. In this case, the dialogue box.

Upvotes: 0

Sunil B N
Sunil B N

Reputation: 4225

prompt itself is a function.. its like you are assigning the return value of prompt function to the variable person

Upvotes: 0

Related Questions