Check If All Values In Array Are Null In JavaScript

To check if all values in array are null in JavaScript, you can use the every() method to iterate over the array, and on each iteration, check if the current element is null or not. If all values in the array are null, then every() will return true, otherwise it will return false.

Let’s discuss this method in detail below.

check if all values in array are null in JavaScript

Check If All Values In Array Are Null In JavaScript

The every() method is used to test if all elements in an array passes the test implemented by the callback function.

This method executes the callback function once for each element present in the array until it finds one where callback returns a false value. If such an element is found, every() immediately returns false. Otherwise, if callback returned a true value for all elements, every() will return true.

Here is an example of how to check if all values in array are null in JavaScript using the every() method.

function isAllNull(arr) {

return arr.every(function(element) {

 return element === null;

});

}

console.log(isAllNull([null, null, null])); // true

console.log(isAllNull([null, 1, 2])); // false

As you can see from the above code, the isAllNull() function takes an array as an argument and returns true if all values in the array are null. Otherwise, it returns false.

You can also use arrow functions to write more concise code as follows.

function isAllNull(arr) {

  return arr.every(element => element === null);

}

console.log(isAllNull([null, null, null])); // true

console.log(isAllNull([null, 1, 2])); // false

You can also use the reduce() method to check if all values in an array are null or not as follows.

function isAllNull(arr) {

  return arr.reduce((acc, element) => acc && element === null, true);

}

console.log(isAllNull([null, null, null])); // true

console.log(isAllNull([null, 1, 2])); // false

The reduce() method is used to apply a function to each element in the array and reduce it to a single value. The above code iterates over the array elements and checks if all values are null or not. If all values are null, then it returns true, otherwise it returns false.

You can also use the some() method to check if all values in an array are null or not as follows.

function isAllNull(arr) {

  return !arr.some(element => element !== null);

}

console.log(isAllNull([null, null, null])); // true

console.log(isAllNull([null, 1, 2])); // false

The some() method is used to test if at least one element in an array passes the test implemented by the callback function. This method executes the callback function once for each element present in the array until it finds one where callback returns a true value. If such an element is found, some() immediately returns true. Otherwise, if callback returned a false value for all elements, some() will return false.

As you can see from the above code, the isAllNull() function takes an array as an argument and returns true if all values in the array are null. Otherwise, it returns false.

You can also use the for loop to check if all values in an array are null or not as follows.

function isAllNull(arr) {

for (let i = 0; i < arr.length; i++) {

 if (arr[i] !== null) {

   return false;

 }

}

return true;

}

console.log(isAllNull([null, null, null])); // true

console.log(isAllNull([null, 1, 2])); // false

As you can see from the above code, the isAllNull() function takes an array as an argument and returns true if all values in the array are null. Otherwise, it returns false.

Conclusion – Check If All Values In Array Are Null In JavaScript

In this article, you have learned how to check if all values in array are null in JavaScript using different methods. You can use the every() method, the reduce() method, the some() method or the for loop to achieve this goal.

I hope this article has been helpful to you. If you have any questions, please feel free to post them in the comments section below.

Leave a Reply