In javascript, a = alert("why?") does the same thing as just alert("why?).
Why? And what is the name of that feature of the language?
In javascript, a = alert("why?") does the same thing as just alert("why?).
Why? And what is the name of that feature of the language?
You're calling a function, which does whatever the function is defined to do, and then assigning the return value of that function to the variable.
Practically every programming language works like this. You need to learn basic programming concepts.
This is called assigning the result of a function to a variable, and is a common way to use functions. If you just do:
alert("Foo");
that's called calling a function.
alert function. a = alert(...) is no different from a = Math.random(). They both call a function, and assign the result to a variable.alert function in particular. It could be setTimeout or whatever... my question is regarding the terminology difference between using the function 'with a variable' rather than 'on its own'.Because you are calling a function when you pass in a parameter. For example, if you wanted a to actually be a variable that points to the alert function, then you would do something like this:
var a = alert;
Ideally, whenever you pass in the parameter, the function gets called. Then you could do something like:
a("Why?");
And it would get called.
The RHS of an assignment is an expression.
A function invocation is an expression. It is executed and then its return value is assigned to the identifier on the LHS.
<variable> = <exp>