0

I want to be able to check if a variable has been declared, but has not been assigned a value. I've searched similar questions and all seem to suggest using

typeof myVar !== 'undefined'

But that always returned false because even when declared its still undefined. These are the results I'm trying to get:

var myVar;    // Variable is  declared.  Test should return TRUE
//var myVar;  // Variable not declared.  Test should return FALSE 
mwill
  • 424
  • 7
  • 21
  • have you checked http://stackoverflow.com/questions/5113374/javascript-check-if-variable-exists-is-defined-initialized – Snackaholic Nov 11 '15 at 14:49
  • yes, I checked that answer. It doesn't work for the scenario above. – mwill Nov 11 '15 at 15:16
  • If you are in control of the code where the variables are declared, you should always initialize them to null, since javascript undefined is just a value that is assigned by the engine when no value was specified, so your test will always fail. – Lu Roman Nov 11 '15 at 15:37

3 Answers3

1

Unfortunately typeof will return you undefined in both cases. The only way I know to find out if the variable is actually defined is by using try-catch. Try this:

var a;
var aExists=true;
var bExists=true;
try{a}catch(e){aExists=false}
try{b}catch(e){bExists=false}
console.log(aExists, bExists);
metal03326
  • 1,213
  • 2
  • 13
  • 15
0

You can do this by using the in operator and checking the existence of a variable in the current context:

var myVarDeclared;
var myVarDefined = '';

var myVarDeclaredTest = 'myVarDeclared' in this;
var myVarDefinedTest = 'myVarDefined' in this;
var notDeclaredTest = 'notDeclared' in this;

document.write('myVarDeclared test: ' + myVarDeclaredTest + '<br>'); //true
document.write('myVarDefined test: ' + myVarDefinedTest + '<br>'); //true
document.write('notDeclared test: ' + notDeclaredTest + '<br>'); //false
Will
  • 2,790
  • 19
  • 22
  • Chrome displays false, false, false. – mwill Nov 11 '15 at 15:57
  • Why would you exclusively check it's existence in this? What about closures? Also it seems that `in` operator goes up the prototype chain to look for properties. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/in – Olga Nov 11 '15 at 15:57
0

If you have a global variable or some property you could use hasOwnProperty method. e.g window.hasOwnProperty('myVar') or object.hasOwnProperty('myVar') it will be true - if a variable/property has been defined.

The typeof operator will not give you such information it as it will return 'undefined' for variables that are not defined at all but you can combine it with previous technique to check if a variable is defined yet not assigned a value. But this can still give you false positive if a variable was first assigned a value and the assigned undefined.

For your local variable (non global variables) although they are properties you cannot reference "their" object. But a call to an undefined variable should result in Exception. So you may try this snippet:

function isDefined(variableName) {
    try {
        eval(variableName);
    } catch (ex) {
        return false;
    }
    return true;
}

eval here is used so the exception will happen inside try catch - not when isDefined is called. And I really advise against using this anywhere near production.

All that said what you want is a little strange - do you really need to check that a variable is defined?

Olga
  • 1,648
  • 1
  • 22
  • 31