JavaScript isset() Equivalent

If you are familiar with PHP’s isset() function, you know that it is used to determine if a variable has been set and is not NULL. If you do not use the isset() function, you risk throwing a PHP error saying that the variable is undefined. Similarly, if you do not use the following JavaScript isset() equivalent, JavaScript will throw an error as well.

The isset() equivalent in JavaScript is the following code:

if (typeof variable_name !== 'undefined') {
  // variable_name has been set, so run this code
}

If you want to check if a JavaScript object’s property exists, run the following code:

if (object_name.hasOwnProperty('action')) {
  // the object_name object has the action property, so run this code
}

Leave a Reply

Your email address will not be published. Required fields are marked *