Monday 13 February 2012

Javascript: check if a variable is defined

var foo = 1; //variable exists

if(! typeof foo == 'undefined') {
  alert("var 'foo' exists!");
}
else {
  alert("var 'foo' doesn't ex
ist!"); }

References

See also how to do the same with jQuery.

No comments:

Post a Comment