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 
3
  • have you checked stackoverflow.com/questions/5113374/… Commented Nov 11, 2015 at 14:49
  • yes, I checked that answer. It doesn't work for the scenario above. Commented Nov 11, 2015 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. Commented Nov 11, 2015 at 15:37

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, I was trying to avoid using try/catch but your solution will definitely work.
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?

Comments

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

2 Comments

Chrome displays false, false, false.
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. developer.mozilla.org/en/docs/Web/JavaScript/Reference/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.