The result of evaluating var t = 1 == 1 ? 1 : 0;
in, say, the Firebug console will be undefined
. However, the value of t
will be 1
as expected. Try outputting t
after the assignment.
Firebug will print the result when the variable declaration is on a separate line:
var t;
t = 1 == 1 ? 1 : 0;
This is because the return value of an assignment operation is the value being assigned. However, when the var
keyword is present, what's returning is the value of the VariableStatement declaration, which behaves as follows:
The production VariableStatement : var
VariableDeclarationList; is evaluated
as follows: Evaluate
VariableDeclarationList. Return
(normal, empty, empty).
Where Return (normal, empty, empty).
refers to a type recognized by JavaScript internally, not something that would be printed to the console.
Further reading:
http://ecma262-5.com/ELS5_HTML.htm#Section_12.2