What is the correct way to check if a global variable exists?

JSLint does not pass this as valid code:

/* global someVar: false */ if (typeof someVar === "undefined") { var someVar = "hi!"; } 

What is the right way?

+43
javascript jslint global-variables
Jul 21 '12 at 22:24
source share
8 answers
 /*global window */ if (window.someVar === undefined) { window.someVar = 123456; } if (!window.hasOwnProperty('someVar')) { window.someVar = 123456; } 
+59
Jul 21 2018-12-21T00:
source share
β€” -
 /** * @param {string} nameOfVariable */ function globalExists(nameOfVariable) { return nameOfVariable in window } 

It does not matter if a global variable was created with var foo or window.foo - variables created with var in the global context are written to the window.

+13
Jul 21 '12 at 22:30
source share

If you want to assign a global variable only if it does not already exist, try:

 window.someVar = window.someVar || 'hi'; 

or

 window['someVar'] = window['someVar'] || 'hi'; 
+12
Jul 21 2018-12-21T00:
source share

I think this is actually a problem with JSLint. It produces the following error:

Unexpected 'typeof'. Compare directly to 'undefined'.

I think this is bad advice. In JavaScript, undefined is a global variable that is usually undefined. But some browsers allow scripts to modify it, for example: window.undefined = 'defined' . If so, comparing directly with undefined may lead to unexpected results. Fortunately, current ECMA 5 compatible browsers do not allow undefined (and throw an exception in strict mode).

I prefer typeof someVar === "undefined" as you wrote, or someVar in window , as Sussey suggested.

+8
Jul 21 '12 at 22:37
source share

try

 variableName in window 

or

 typeof window[variableName] != 'undefined' 

or

 window[variableName] !== undefined 

or

 window.hasOwnProperty(variableName) 
+7
Jul 21 '12 at 22:35
source share
 if (typeof someVar === "undefined") { var someVar = "hi!"; } 

will check if someVar (local or global) is undefined.

If you want to check the global variable, you can use

 if(window['someVar'] === undefined) { ... } 

Suppose this is in the browser :)

+4
Jul 21 2018-12-21T00:
source share

bfavaretto is invalid.

Setting the global undefined value to a value will not change the object tests to undefined. Try this in your favorite browsers. JavaScript console:

 var udef; var idef = 42; alert(udef === undefined); // Alerts "true". alert(idef === undefined); // Alerts "false". window.undefined = 'defined'; alert(udef === undefined); // Alerts "true". alert(idef === undefined); // Alerts "false". 

This is because JavaScript ignores all and any values ​​that you tried to set in the undefined variable.

 window.undefined = 'defined'; alert(window.undefined); // Alerts "undefined". 
+2
Sep 24
source share

This would be an easy way to do the validation.

But this check will not work if variableName declared and assigned using boolean value: false

 if(window.variableName){ } 
+1
May 13 '14 at 6:39
source share



All Articles