“var variable” returns undefined?

前端 未结 5 808
忘掉有多难
忘掉有多难 2020-12-11 03:44

When I run \"var variable = true;\" in chrome console I get \"undefined\" returned:

> var variable = true;
undefined

But when I run with

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-11 03:56

    var x = y; is a statement which returns no value. In the WebKit JS console, a statement that returns no value will show undefined as the result, e.g.

    > if(1){}
    undefined
    > ;
    undefined
    > if(1){4}  // this statement returns values!
    4
    

    The assignment is an expression which returns the value of the LHS. That means, this expression statement has a return value, and this will be shown.

提交回复
热议问题