0

I need to pass a list of variable names in JavaScript and check if those variables exist. I tried the following but it doesn't seem to be doing the trick (JSFiddle here):

var test1 = 'test1';
var test2 = 'test2';

function checkVariable(variableNameList) {
    for (var iterator = 0; iterator < variableNameList.length; iterator++) {
        var variableName = variableNameList[iterator];
        if (typeof window[variableName] === 'undefined') {
            alert('Variable ' + variableName + ' is not defined');
        } else {
            alert('Variable ' + variableName + ' is defined');          
        }
    }
}

checkVariable(['test1', 'test2', 'test3']);

I'm trying to get the resulting alerts:

  1. Variable test1 is defined.
  2. Variable test2 is defined.
  3. Variable test3 is not defined.

It seems easy to fix using the trick below but is there any other way to achieve this? Is declaring global variables under window the only way to track them?`

window.test1 = 'test1';
window.test2 = 'test2';

Are there better ways to do this or is this the right approach?

Vanilla JS only answers please.

Community
  • 1
  • 1
Nicolas Bouvrette
  • 4,295
  • 1
  • 39
  • 53
  • 1
    Why is using `window.varname` a trick? Global variables are defined on the window object. – James Hibbard Dec 28 '15 at 19:27
  • What are you trying to do exactly? – epascarello Dec 28 '15 at 19:28
  • @JackZelig Then why isn't this working without explicitly declaring the variable to window? – Nicolas Bouvrette Dec 28 '15 at 19:29
  • 1
    https://jsfiddle.net/b5t4p2d4/2/ you werent assigning the vars to the window obj so you cant reference them. if you want to set a window var or any other window var, use `obj.property=''` – David Dec 28 '15 at 19:30
  • @epascarello I'm trying to verify is some variable exists in a dynamically loaded script file before executing some code. But my other variables were not defined under `window` so I was curious if this was the right approach. – Nicolas Bouvrette Dec 28 '15 at 19:30
  • 1
    In your fiddle you use onload event so variables are local to onload function, try this https://jsfiddle.net/b5t4p2d4/3/ – jcubic Dec 28 '15 at 19:31
  • 3
    @James Hibbard, not all JavaScript runs in `window`. Workers have no `window`. node has no `window`. And in fact you can make a function `function foo(window) { ... } `. Inside foo you'd still like to know if the global variable exists but if it's called like this `foo(undefined)` the check via window will fail. One solution is to use `typeof` `if (typeof someVar !== 'undefined')` then `someVar` exists. – gman Jan 29 '19 at 11:49

1 Answers1

5

It does not work because the variables are not in global scope, they are in scope of the window.onload function scope.

enter image description here

Your code is actually running like this:

window.addEventListener("load", function () {
    var test1 = 'test1';  /* these are not global because of */
    var test2 = 'test2';  /* running inside of window.onload */

    function checkVariable(variableNameList) {
    }

    checkVariable(['test1', 'test2', 'test3']);
});

Change your code to run either in the head or at the end of the body. I forked your code to run in the head: https://jsfiddle.net/phrhxyzL/1/ and you get the results you expect.

epascarello
  • 204,599
  • 20
  • 195
  • 236