Why does the browser return undefined?

I found a phenomenon that confused me when I run simple javascript code from the browser console (Chrome and Firefox).

When I typed the word

>var a = "a"

The browser will return a string

>"undefined"

but if I just typed

>a = "a"

The browser will return a string

>"a"

Why is this the case?

+4
source share
2 answers

If you write

alert(var a = 'a')

You get a syntax error, varis part of the javascript syntax, it returns nothing.

However, the part a = 'a'returns something.

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

And d = 'e'returns ewhich gets in c=d, which really c = 'e', etc. After you go to var, it will stop returning the value.

var a;, undefined. var a = 'b' var a; a = b;

+4

.

var a = 'a'

, undefined.

b = 'b' 

, .

var a = b = c = d = 'foo';

undefined, . , , .

+3

Source: https://habr.com/ru/post/1524985/


All Articles