undefined thrown when defining var

后端 未结 2 876
萌比男神i
萌比男神i 2021-01-28 22:25

The next code is interpreted in Google-chrome console:

a = 123
123
     % Ok!

var b = 123
undefined
     % `undefined`? why? b is not undefined, it contains `12         


        
2条回答
  •  遇见更好的自我
    2021-01-28 22:50

    var is a statement. Statements have no value, so eval() (which the console calls) returns undefined.

    a = 123 is a simple expression, which returns 123 (so that you can write b = a = 123). When passed an expression, eval() returns the expression's value.

提交回复
热议问题