Count number of null values in an array in Javascript

In this tutorial, we will learn how to check null values in an array in javascript. This guide provides clear step by step instructions to count number of null values in Javascript

Introduction

In javascript, null is a special value that represents an empty or unknown value. We use a triple equality operator === to check null. We can find the null values using ‘filter()’ and ‘reduce()’ method.

Filter() method to count null values in an array in javascript

function Nullvalues(arr) // Function Nullvalues takes an array as input
{
    function isNull(value) // Helper function to check if the value is null
    {
        return value === null; // Returns true if the value is null, otherwise returns false
    }
    
     nullCount = arr.filter(isNull).length; // Filters the array to form a new array containing only null values
    console.log("Number of null values:", nullCount); // Prints the number of null values to display the output
}

 array = [1, 4, 5, null, 2, 6, null, "ABC", null, "xyz", 7]; // Array is defined with various values including null
Nullvalues(array); // Calls the function to initiate the execution
Explanation:

We initiate the process by calling a function named ‘Nullvalues’ that accepts an array as its argument. Inside the function we utilize  another function named ‘isnull’ to determine whether the given value is null or not. If the value is null then it returns true otherwise it returns false.
In the above code we are taking an example of array[1,4,5,null,2,6,null,”ABC”,null,”XYZ”,7] which signifies declaring an array by using a mix of numbers, strings and null values. Then we filter the array using ‘isnull’ function to create a new array consisting of the element that are null.

Then it gives the length of the filtered array which represents the count of null values in the original array. we display the output using console.log. Finally, we call the function Nullvalues again with the array argument, initiating the execution of the function.

Output:
Number of null values: 3

https://drive.google.com/file/d/16bO4lmM8KA4b8CAjpqxR7-CMuQp74gw5/view?usp=drivesdk

 Reduce() method to count null values in an array in javascript

function Nullvalues(arr) // Function Nullvalues takes an array as input 
{
   nullcount = arr.reduce((acc, currentValue) => // Using the reduce method to count the null values
  {
    // If the current value is null, increment the accumulator, otherwise keep it unchanged 
    return currentValue === null ? acc + 1 : acc; 
  }, 0); // Start with an initial value of 0 for the accumulator

  return nullcount; // Return the count of null values 
}

 array = [1, 2, null, "A", 3, null, "B", null, 4, 5]; // Array is defined 
 nullCount = Nullvalues(array); // Call the function to count the null values in the array
console.log("Number of null values:", nullCount); // Display the output
Explanation:

We call a function Nullvalues that takes an array as an argument. The reduce method checks each element of the array to see if it’s null. If it is null, it increments the accumulator by 1; otherwise, it leaves the accumulator unchanged. Then the function returns the final count of null values after the reduce operation is done.
In the above code, we use an example array [1, 2, null, “A”, 3, null, “B”, null, 4, 5], which contains a combination of numbers, strings, and null values. The Nullvalues function is called with the array argument, and the result is stored in the variable nullcount. The console.log displays the output.

output:
Number of null values:3

https://drive.google.com/file/d/16j2xNOYeHs7ub9KKYMSwIFp2gbNY39kr/view?usp=drivesdk

Leave a Comment

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

Scroll to Top