javascript isset()

Have you ever wanted to test the existance of a javascript variable
(an array item for example) ?
Just like in PHP, I’ve found a method to do this.

The function returns true if the variable is set or false if the variable was not set.
A variable to be set must be initialized.
The starting point was that an unitialized variable has the type ‘undefined’.

A basic method can look like this:

function isset(varname){
return(typeof(varname)!=‘undefined’);
}
 

Some problems appear with this implementation, as Glen reported.

One problem is that when you pass an undefined variable, javascript throws an exception.
The other problem is that function member names can mess up the detection. (ex: pop() for array)
So here is a modified version. Please note the try/catch trick:

<script type="text/javascript">

function isset(varname)
{
try
{
var t = typeof(varname);
}
catch(e) {return false;}

if(t!==‘undefined’ && t != ‘function’) return true;
else
 return false;
}

var global_var = new Array();

function test()
{
var local_var = "xx";

try
{         
document.writeln("Test for function name (should be false) global_var[\"pop\"].Answer: " + isset(global_var["pop"]) + "<br>"); // returns false – as global_var.pop() is a function
}
catch(e) {alert(e) /*code for not set*/}

try
{         
document.writeln("Test for global var (should be true).\nAnswer: " + isset(global_var) + "<br>"); // returns true as global_var is set
}
catch(e) {alert(e) /*code for not set*/}

try
{         
document.writeln("Test for defined local var (should be true).Answer: " + isset(local_var) + "<br>"); // the script does not support testing local vars… always returns false
}
catch(e) {alert(e) /*code for not set*/}

try
{         
document.writeln("Test for bogus var which is not defined (should be false).Answer: " + isset(bogus) + "<br>"); // returns false as bogus var is not defined
}
catch(e) {alert(e) /*code for not set*/}

}

test();

</script>
 

stumbleupon digg rss

2 People have left comments on this post



» Glen said: { Jun 28, 2008 - 07:06:18 }

That doesn’t work all the time. Function prototypes mess things up, for instance:

var a = new Array();
isset(a['pop']); // returns true – as a.pop() exists

your routine will work for these cases (there are many) with this change:

function isset(varname)
{
var t=typeof(varname);
return(t != ‘undefined’ && t != ‘function’);
}

» Glen said: { Jun 28, 2008 - 08:06:56 }

Forgot to mention… if you are testing for the existence of a global variable like this:

// var global; // note it is commented out
isset(global);

.. will result in a ‘global is not defined’ JS exception thrown. Instead, you need to remember to do either one of these:

isset(window.global); // these are equivalent
isset(window['global']); // these are equivalent

I don’t think there is any way to code for this in the isset() routine, since the exception is thrown before isset() is called. JS doesn’t seem to mind passing an undefined reference, as long as the highest-level piece (i.e. ‘window’ in this case) does exist. BTW, this will work the same if trying to test for the existence of a local variable … like

function func()
{
isset(loc); // will also throw an exception
}

since (i think) you can’t specifically reference local variables without targeting globals (though you can specifically reference globals via window.variable_name).

This is pretty basic stuff. Do we really have to go to these lengths?


Post a Comment