Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: z = z || [] throws an error when not using VAR - why?

Out of just intellectual curiosity, why does javascript accept

var z = z || [];

to initialize z (as z may defined initially)

but without var, it throws an error (in global space)

z = z || [];

(if z is previously undefined)

In the global space you are not required to use VAR though I get it might be bad practice.

Before you say this is a duplicate of questions like

What is the purpose of the var keyword and when to use it (or omit it)?

Note the declaration that "If you're in the global scope then there's no difference."

Obviously that's not 100% true, given my working example.

Is this a quirk or is there legitimate logic?


adding a summary of the answer as I've learned it:

Thanks to Tim (see below) the key to my misunderstanding was not realizing this (fundamental of javascript)

var z; does absolutely nothing if z already exists

That's how this expression seems to have it both ways, if you incorrect assume that "var z" always initializes.

Starting from the left, "var z" simply makes sure z is defined but does not actually affect any existing value if it already exists. Then on the right, if z already exists, it is used, if not, the variable was just declared (but empty) so it won't be used but will not throw an error.

This is an excellent article on this kind of scoping and hoisting issue in Javascript: http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting

Many thanks to minitech and everyone else who contributed too!

like image 827
ck_ Avatar asked Jan 30 '12 16:01

ck_


People also ask

Why we should not use VAR in JavaScript?

This means that if a variable is defined in a loop or in an if statement it can be accessed outside the block and accidentally redefined leading to a buggy program. As a general rule, you should avoid using the var keyword.

What happens if you use a named variable without first declaring it using the VAR let or const keywords?

If you declare a variable, without using "var", the variable always becomes GLOBAL.

Why would you use VAR in JavaScript?

The var keyword is used in all JavaScript code from 1995 to 2015. The let and const keywords were added to JavaScript in 2015. If you want your code to run in older browsers, you must use var .

Should I stop using var JavaScript?

If you are new to JS or been with it for quite some time then for the most part you probably be using var to declare your variables which is fine but it's not the most efficient & preferred way of declaring variables.


1 Answers

The effect is correct. var will always declare its "operands" right away, whereas when you don't declare it, your script attempts to use an undefined variable and throws an error.

If you're in global scope, you can assign to a nonexistent variable and it will have the same effect as declaring it, bad practice as that may be. Of course, in your case, it's undefined. This being said, although it may be out of intellectual curiosity, you would never write

var z = z || [];

because it makes no sense to do so. Rather, you might do:

if(!window.z) {
    window.z = [];
}

. In fact, when I declare things in the global scope (which is never ;)) I use window.something instead because it makes my intent more clear.

like image 112
Ry- Avatar answered Sep 21 '22 02:09

Ry-