Detecting if a variable exists in JavascriptIn Javascript there is no explicit function you can use to see if a variable exists. And to make matters worse, if you try to use a variable that does not exists already, you'll get an error in your script.
The easiest way to check for the existence of a variable in Javascript is to make use of the typeof operator as it can take undefined variables as input. The typeof operator will always return a string value, and if the value it is given is an undefined variable the string returned will be "undefined". So you can test for the existence of a variable like so: if ( typeof(variableName) == "undefined" ) {
// Action to take if variableName is not defined
} else {
// Action to take if variableName *is* defined
}
|

