6

I would like to find out if a Javascript variable exists. This is what I have so far which was cobbled together from different forums:

function valueOfVar(foo){

    var has_foo = typeof foo != 'undefined';

    if(has_foo){
        alert('1 = true');
        return true;
    }
    else {
        alert('1 = false');
        return false;
    }

}

Please note, I wish to pass in a string as foo. Example: valueOfVar(box_split[0]+'_2')

Now, I don't think this works because it returns true when certain variables don't even exist. In fact, it seems to return true all the time.

A JQuery implementation that works would also be great as I make use of this.

Thanks all for any help

Abs
  • 56,052
  • 101
  • 275
  • 409

3 Answers3

6

Do you mean something like this?

function variableDefined (name) {
    return typeof this[name] !== 'undefined';
}

console.log(variableDefined('fred'));
// Logs "false"

var fred = 10;
console.log(variableDefined('fred'));
// Logs "true"

If you want to be able to handle local variables you'll have to do something quite weird like this:

function variableDefined2 (value) {
    return typeof value !== 'undefined';
}

function test() {
    var name = 'alice'
    console.log(variableDefined2(eval(name)));
    var alice = 11;
    console.log(variableDefined2(eval(name)));
}

test();
// Logs false, true
Rich
  • 3,095
  • 17
  • 17
  • It returns true on all cases still! – Abs Feb 20 '10 at 23:11
  • It doesn't when I test it in Chrome's Javascript console. How are you testing it? – Rich Feb 20 '10 at 23:13
  • Yes, it worked! Fantastic! The mistake was that I just didn't change name to foo...The variable I was using. Thank you Rich. :) – Abs Feb 20 '10 at 23:22
  • 1
    I'm not sure what `this` is supposed to achieve here... you're calling it as a plain function, so `this` will be the `window` object. Which is OK for testing the existance of globals (since they're properties of `window` too), but it would probably be better to be explicit about it and just say `window`. Local variables cannot be checked for using either version (or at all), but it's not really clear what Abs is trying to do here... there is almost always a better way than variable-variables. – bobince Feb 20 '10 at 23:55
  • Typing "this" is quicker than typing "window", and you could also then test for properties of an object by using Function's "call" or "apply" methods. Also, it is possible to check for local variables but it's quite ugly as you need to "eval" the argument before calling the function (see my edited version). – Rich Feb 21 '10 at 00:24
1

The problem with your valueOfVar(box_split[0]+'_2') test is that you are appending a string to the undefined attribute. You would always be passing a string to valueOfVar() with the value of 'undefined_2'.

You will notice that the typeof operator will return 'string' if you try the following:

function valueOfVar(foo){
    alert(typeof foo);
}

valueOfVar(box_split[0]+'_2');

The typeof operator would work for these kinds of tests, but you cannot append anything to the undefined variable, otherwise it would test always true:

if (typeof foo === 'undefined') {
  // foo does not exist
}
else {
  // it does
}
Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
  • box_split[0] is defined. Why did you think it was undefined? It has a value of "chk". – Abs Feb 20 '10 at 23:19
0

Not sure if this addresses your problem, but in JavaScript, null is not the same thing as undefined. The code you wrote is correct for testing if a variable isn't defined. IE:

<script>
window.onload = function()
{
     alert(typeof(abcd) == "undefined"); //true
     abcd = null;
     alert(typeof(abcd) == "undefined"); //false
};
</script>

By the way, the type of null is "object".

Warty
  • 7,237
  • 1
  • 31
  • 49