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()in JavaScript

The Object.keys() is a method in JavaScript that returns an array of the keys of an object. It takes one parameter, which is the object whose keys are to be returned. The order of the keys in the array is based on how they were added to the object; newer properties will appear after older ones. Object.keys can also be used for arrays, since arrays are objects as well and each element in an array has its own key (index).

Syntax

Following is the syntax for the Object.keys() method.

Object.keys(object)

Example

In the following example we are running the script using Object.keys.

<!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: ', i + "<br>");
         else
         document.write(' data right', i);
      });
   </script>
</body>
</html>

When the script is executed, the event is triggered, allowing us to check the entire data set that was used in the script to determine whether the null present is present or not, and it will display null present in the first set of data and actual data in the second set of data right on the webpage based on our data.

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

Following is the syntax for some() method

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

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( arr.some(hasNull) );
   </script>
</body>
</html>

On running the above script, the web-browser will display the value "true" on the webpage as the event gets triggered and checks whether the given array consists of null values or not, and it will display "true" as the condition was matched and the given array consists of null values.

Using JavaScript include() Method

The JavaScript include() method is used to load an external script or file into the current document. This method allows you to add scripts, libraries, and other files from outside of the existing HTML page. It can be used with both inline and external scripts. The include() method will execute the code in the specified file before continuing with the rest of your program.

Syntax

Following is the syntax for include()

string.includes(searchvalue, start)

Example

Let’s look into the following example where we are using the include() method

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

When the script gets executed, the event gets triggered, which checks the data entered in the script to see whether, it contains null value or not and displays the value on the webpage. In the above case, it shows that the array does not contain null value and the object contains null value.

Updated on: 18-Jan-2023

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements