During JavaScript application development, you will often encounter the request to check if all values in array are null. This article will share how to check if all values in array are null in JavaScript. Let’s get started.
How to check if all values in array are null in JavaScript
The solution in this article to check if all values in an array in JavaScript are null is to use the Array.every()
method. First, we will learn about every()
method, its syntax, and how to use it.
The every()
method is used to check all the elements in the input array using a provided callback function. The every()
method will return true if all elements pass the test.
Syntax:
every(Callback function)
Parameter:
Callback function: A function is called again for each element. This function performs the element test and returns true if the element passes the test.
If the callback function returns a false value, the every()
method will stop checking and return false. The every()
method returns true if the input array is empty.
To check if all the values in the array are null using every()
method, we pass this method a callback function that checks if the array element is null. The following code describes how we do this.
Example:
// Check if all elements in the array are null function isNull(array) { return array.every((value) => value === null); } // Create an array const nullArr = [null, null, null]; console.log(isNull(nullArr)); const numArr = [2, 1, 0, 6, null, 12]; console.log(isNull(numArr));
Output:
true
false
Another solution is to use every()
method in combination with the NOT operator (!). This solution can work for all falsy values.
We will use the NOT operator to convert the values in the array to boolean values. The NOT operator will return true for falsy values. If all values in the array are falsy, the every()
method will return true.
Example:
// Check if all elements in the array are null function isNull(array) { return array.every((value) => !value); } // Create an array const nullArr = [null, null, null]; console.log(isNull(nullArr)); const falsyArr = [false, "", 0, null, undefined, NaN]; console.log(isNull(falsyArr)); const numArr = [2, 1, 0, 6, 13, 12]; console.log(isNull(numArr));
Output:
true
true
false
Summary
This article shared how to check if all values in an array are null in JavaScript. You can use every()
method with the logical NOT operator. Your code will work correctly for other falsy values. Thank you for reading.

Hello, my name’s Bruce Warren. You can call me Bruce. I’m interested in programming languages, so I am here to share my knowledge of programming languages with you, especially knowledge of C, C++, Java, JS, PHP.
Name of the university: KMA
Major: ATTT
Programming Languages: C, C++, Java, JS, PHP