gautamlakum gautamlakum - 1 month ago 4
Javascript Question

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

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

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();
}