Why does browser returns undefined?

javascript

Solution

If you write

alert(var a = 'a')

You get a syntax error, `var` is part of the javascript syntax, it doesn't return anything.

The `a = 'a'` portion, however, does return something.

You can do `var a = b = c = d = 'e';`

And the `d = 'e'` returns `e`, which gets fed into the `c=d` which is really `c = 'e'`, etc. Once you get to the var it stops returning the value.

If you type `var a;` you get undefined. `var a = 'b'` is essentially just shorthand for `var a; a = b;`

Problem

I have found a phenomenon that confused me when I run simple javascript code from the browser console(Chrome & Firefox). When I typed say ``` >var a = "a" ``` The browser will return a string ``` >"undefined" ``` but if I just typed ``` >a = "a" ``` The browser will return the string ``` >"a" ``` Why is this case?

Original source

Related problems