is there something like isset of php in javascript/jQuery?

gautamlakum picture gautamlakum · Nov 20, 2010 · Viewed 146.1k times · Source

Is there something in javascript/jQuery to check whether variable is set/available or not? In php, we use isset($variable) to check something like this.

thanks.

Answer

Emil Vikström picture Emil Vikström · Nov 20, 2010

Try this expression:

typeof(variable) != "undefined" && variable !== null

This will be true if the variable is defined and not null, which is the equivalent of how PHP's isset works.

You can use it like this:

if(typeof(variable) != "undefined" && variable !== null) {
    bla();
}