Is there any way to check if there is a null value in an object or array in JavaScript?

Null values can be tricky to work with in JavaScript, but they are an important part of the language. In this article, we will discuss various ways you can check if there is a null value in an object or array in JavaScript.

The null values display that no object value is present. It is intentionally set to show that a variable has been declared but has not yet been given a value.

The primitive value is undefined, which is an unintended absence of any object value, which is comparable to null, that contrasts with the former. This is due to the fact that a declared variable that has not yet been given a value is undefined rather than null. There are many ways to check whether a value is null or not in JavaScript. Let's discuss few ways one by one.

Using Object.keys() and some() Methods

The Object.keys() method returns an array of the keys of an object. Combined with some(), we can check if any property contains null values. This approach works well for objects and arrays of objects.

Syntax

Object.keys(object)

Example

In the following example we are running the script using Object.keys() with some().

<!DOCTYPE html>
<html>
<body>
   <script>
      var data = [{
         name: "AVATAR",
         Car: "AUDI",
         Bike: null,
         Location: null
      }, {
         name: "RAM",
         Car: "No",
         Bike: 'BULLET',
         Location: 'LA'
      }];
      
      data.forEach(function(v, i) {
         if (Object.keys(v).some(function(k) {
               return v[k] == null;
            })) {
            document.write('Contains null value at index: ', i, "<br>");
         } else {
            document.write('No null values at index: ', i, "<br>");
         }
      });
   </script>
</body>
</html>
Contains null value at index: 0
No null values at index: 1

Using JavaScript some() Method

The some() method accepts a function as a parameter and tests whether at least one element in the array passes the test implemented by the given function. It returns true when an element for which the provided function returns true; otherwise it returns false.

Syntax

array.some(function(value, index, arr), thisArg)

Example

Considering the following example, where we are using the some() method to check whether null is present or not.

<!DOCTYPE html>
<html>
<body>
   <script>
      var arr = [
         { x: "1", y: "2", z: "3" },
         { x: "ram", y: "abc", z: "var" },
         { x: "abc", y: "def", z: null }
      ];
      
      function hasNull(element, index, array) {
         return element.x === null || element.y === null || element.z === null;
      }
      
      document.write("Array contains null: ", arr.some(hasNull));
   </script>
</body>
</html>
Array contains null: true

Using JavaScript includes() Method

The JavaScript includes() method determines whether an array includes a certain value among its entries, returning true or false. For objects, we can use Object.values() to get all property values and then check if null is included.

Syntax

array.includes(searchElement, fromIndex)

Example

Let's look into the following example where we are using the includes() method:

<!DOCTYPE html>
<html>
<body>
   <script>
      var nameArray = ["Jack", "rose", "Sam"];
      if (nameArray.includes(null) == true) {
         document.write("Array contains null value<br>");
      } else {
         document.write("Array does not contain null value<br>");
      }
      
      var objectData = { name: null, age: 25 };
      if (Object.values(objectData).includes(null)) {
         document.write("Object contains null value<br>");
      } else {
         document.write("Object does not contain null value<br>");
      }
   </script>
</body>
</html>
Array does not contain null value
Object contains null value

Comparison of Methods

Method Best For Performance Browser Support
Object.keys() + some() Objects Good ES5+
some() Arrays of objects Best ES5+
includes() Simple arrays Good ES6+

Conclusion

Use includes() for simple arrays, some() for complex array checks, and Object.values() with includes() for objects. Choose the method that best fits your data structure and browser compatibility requirements.

Updated on: 2026-03-15T23:19:00+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements