Check this interactive Google Chrome console log:
test_1 = 'ok'
> "ok"
test_2 = test_2 || 'ok'
> ReferenceError: test_2 is not defined
var test_3 = test_3 || 'ok'
> undefined
test_1
> "ok"
test_2
> ReferenceError: test_2 is not defined
test_3
> "ok"
When I call test_1 = 'ok'
I leave out the var
constructor, but the browser still understands this. I assume it fills in with the var
where I omitted, just like it fills in with semicolons.
But for test_2 = test_2 || 'ok'
I get an error. I know test_2
is not defined but it doesn't keep my next example test_3
from working. For some reason the missing var
statement becomes a problem.
Can somebody explain to me why the interpreter throws an error there?