You can use the typeof operator to check if a variable is undefined in JavaScript. For example:

if (typeof variableName === 'undefined') {
  // variable is undefined
}

To check if a variable is null, you can use a simple comparison:

if (variableName === null) {
  // variable is null
}

You can also check both in a single line using logical OR operator :

if(variableName === undefined || variableName === null) {
  // variable is undefined or null
}

Note that null is a value and undefined is a value as well as type.

Also Read:

Categorized in: