Javascript Check If an Object Is Null or Undefined

While working in javascript, often we encounter a requirement that is to determine if the object is null or not. We might require this check to verify that the object is not null when the objects are loaded dynamically. If the value of the object is not null or not undefined, only then perform operations on it. This article discusses various ways to check if an object is null or undefined using example illustrations.

Javascript objects are something that describes anything with properties and methods.

The null keyword in javascript primitive data means that there is no value. If an object is null, it will not have any value.

The undefined keyword in javascript means not defined. If an object is undefined, it means that it has not been assigned any value.

Example:-

Advertisements

Check if the below objects are null or undefined

  • personObject1 = { personFirstName : ‘George’, personLastName : ‘Smith’, dateOfBirth : ‘Nov 14 1984’ , city : ‘Santiago’ }
  • personObject2
  • personObject3 = null
  • personObject4 = {}

Code:-

// function to check if the object is undefined or null
function checkIfObjectIsNull(_object)
{
if (_object === undefined || _object === null) {
console.log("Object Is Null Or Undefined");
}
else
{
  console.log("Object Is Ok");
}
}

let personObject1 =  { personFirstName : 'George',
                      personLastName  : 'Smith',
                      dateOfBirth     : 'Nov 14 1984' ,
                      city            : 'Santiago'
                    };
let personObject2 ;
let personObject3 = null ;
let personObject4 = {};
// usage of the function to check for the objects
checkIfObjectIsNull(personObject1);
checkIfObjectIsNull(personObject2);
checkIfObjectIsNull(personObject3);
checkIfObjectIsNull(personObject4);

Output:-

Object Is Ok
Object Is Null Or Undefined
Object Is Null Or Undefined
Object Is Ok

Explanation:-

  • Here, we are checking 4 different objects. Let’s see how the results differ.
  • personObject1 is not null nor undefined hence none of the checks are passed in the if statement (_object === undefined || _object === null).
  • personObject2 is not null but undefined as the object is declared, but no value is assigned to it. Doing any operation on it will give an error that the object is undefined. Hence, it passes the check (_object === undefined)
  • personObject3 is null. We are explicitly assigning the value null to the object. Hence, it passes the check (_object === null)
  • personObject4, for this object, we get the output ‘Object Is Okas the object is defined and is not null. Here the object is empty. That is there are no properties and methods defined in this object. Hence, none of the checks are passed in the if statement.

Using typeOf to check if the object is null or undefined

Javascript’s typeOf operator will return a string representing the type of the operand.

Syntax:-

typeof op
typeof(op)

Here op is the operand whose type is determined.

Example:-

Check if the below objects are null or undefined

  • personObject1 = { personFirstName : ‘George’, personLastName : ‘Smith’, dateOfBirth : ‘Nov 14 1984’ , city : ‘Santiago’ }
  • personObject2
  • personObject3 = null
  • personObject4 = {}

Code:-

// function to check if the object is undefined or null
function checkIfObjectIsNull(_object)
{
if (typeof _object === "object" && _object !== null) {
console.log("Object Is Ok");
}
else
{
  console.log("Object Is Null or Undefined");
}
}

let personObject1 =  { personFirstName : 'George',
                      personLastName  : 'Smith',
                      dateOfBirth     : 'Nov 14 1984' ,
                      city            : 'Santiago'
                    };
let personObject2 ;
let personObject3 = null ;
let personObject4 = {};
// usage of the function to check for the objects
checkIfObjectIsNull(personObject1);
checkIfObjectIsNull(personObject2);
checkIfObjectIsNull(personObject3);
checkIfObjectIsNull(personObject4);

Output:-

Object Is Ok
Object Is Null or Undefined
Object Is Null or Undefined
Object Is Ok

Explanation:-

  • Here, the output for personObject1 and personObject4 is “Object Is Ok” because:
  • personObject1 gets passed for both the checks (typeof _object === “object” && _object !== null) as it has the object declared and defined.
  • personObject2 is not null but undefined, hence is not able to pass (typeof _object === “object” ).
  • personObject3 is null and hence fails the check (_object !== null). Here we are using ‘!==’ operator which is a strict equality operator to confirm that the variable is not null.
  • personObject4, since this object is only empty it is able to pass both the checks of the if statement.

I hope this article helped you check if the object is null or undefined. Good Luck !!!

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top