I was reading the 'learning Node' book and I was stuck in a very simple issue, one that I haven't given too much thought about: assignment in javascript.
The author states that we should realize that by using the Node's REPL, the following would return undefined:
var a = 2
(undefined)
while the code below will return '2' in the REPL:
a = 2
2
why is that? the code right above is not an attribution? how e? If the var 'a' hasn't existed until that point in the code, how e it is not and attribution?
I was reading the 'learning Node' book and I was stuck in a very simple issue, one that I haven't given too much thought about: assignment in javascript.
The author states that we should realize that by using the Node's REPL, the following would return undefined:
var a = 2
(undefined)
while the code below will return '2' in the REPL:
a = 2
2
why is that? the code right above is not an attribution? how e? If the var 'a' hasn't existed until that point in the code, how e it is not and attribution?
Share Improve this question edited Nov 29, 2024 at 18:08 dumbass 27.2k4 gold badges36 silver badges73 bronze badges asked Nov 1, 2012 at 15:22 DraconarDraconar 1,1951 gold badge17 silver badges37 bronze badges 3- Have a look at stackoverflow./questions/6297332/… . – moonwave99 Commented Nov 1, 2012 at 15:31
- 1 Why is there a 'browser' tag on this question? – Lior Commented Nov 1, 2012 at 15:31
- the JS REPL in firefox (firebug), chrome and IE > 8 give the same results – Draconar Commented Nov 1, 2012 at 16:11
4 Answers
Reset to default 8Per ECMA-262 § 12.2, a VariableStatement (that is, var identifier=value
) explicitly returns nothing. Additionally, a VariableStatement is a Statement; Statements do not return values, and it is invalid to put a Statement somewhere an Expression would go.
For example, none of the following make sense because they put a Statement where you need value-yielding Expressions:
var a = var b;
function fn() { return var x; }
Per § 11.13.1, assignment to a variable (identifier=value
) returns the assigned value.
When you write var a = 1;
, it declares a
and initalizes its value to 1
. Because this is a VariableStatement, it returns nothing, and the REPL prints undefined
.
a=1
is an expression that assigns 1
to a
. Since there is no a
, JavaScript implicitly creates a global a
in normal code (but would throw a ReferenceError
in strict mode, since you're not allowed to implicitly create new globals in strict mode).
Regardless of whether or not a
existed before, the expression still returns the assigned value, 1
, so the REPL prints that.
Just guessing here - this could probably be verified by referring to the ECMAScript 5th Edition spec (but man that thing is a pain) - it probably has to do with the specification of the "var" statement vs assigning attributes to the "global" object.
When you declare a variable and assign a value to it (var a=2
) the returned value is likely "undefined" because that's what the spec says the "var" statement should return.
When you assign a variable to a symbol without using the "var" statement you are actually assigning a value to an attribute of the global object of that name. That is, a=2
is the same as saying window.a=2
and we know that assinging a value to an attribute returns the value assigned.
var a = 2
is a statement. Thus it has no value.
You are evaluating a statement list. When evaluating a statement list, the value of the last value-producing statement is returned. http://ecma-international/ecma-262/5.1/#sec-12.1 - note the examples at the end of this section. If no statements in the list returned a value, then nothing will be returned (this is undefined
in JavaScript).
The variable statement, does not return a value. http://ecma-international/ecma-262/5.1/#sec-12.2
The assignment operator, does return a value (as well as performing the assignment). http://ecma-international/ecma-262/5.1/#sec-11.13.1