This article will share solutions to check if an array contains undefined in JavaScript using the Array.some()
or Array.includes()
methods. Let’s get started.
How to check if an array contains undefined in JavaScript
We will share the following two solutions to check if an array contains undefined in JavaScript:
- Using the
Array.some()
method - Using the
Array.includes()
method
Now, we will go to each solution with specific examples.
Using the Array.some() method
The some()
method checks if the input array contains any elements that pass the test provided by the callback function.
If an element passes the test, the some()
method will return true and stop. If the callback function returns false for all elements, the some()
method will return false.
The some()
method does not work with empty array elements.
Syntax:
array.some(function)
Parameter:
function: a function to run for each array element.
To check if an array contains undefined using the some()
method. We will give the some()
method a callback function to check if the element is undefined.
Example:
// Create an array const theArr = ["Learn", "Share", "IT", undefined]; // Check if the 'theArr' array contains undefined console.log(theArr.some((value) => value === undefined));
Output:
true
Using the Array.includes() method
The includes()
method checks if an array contains the specified value.
The includes()
method will return true if the input array contains the specified value.
Syntax:
array.includes(element, start)
Parameters:
element: the value to search.
start: start position. The default is 0.
The following code describes how we use the includes()
method to check if an array contains an undefined element.
Example:
// Create an array const theArr = ["Learn", "Share", "IT", undefined]; // Check if the 'theArr' array contains undefined console.log(theArr.includes(undefined));
Output:
true
In many cases, the empty element is also considered undefined because it has not been declared.
The includes()
method will work with empty elements and treat them as undefined.
Example:
// Create an array const theArr = ["Learn", , "Share", "IT"]; // Check if the 'theArr' array contains undefined console.log(theArr.includes(undefined));
Output:
true
We have built a reuse function for this solution to reuse the code.
Example:
// Check if the 'theArr' array contains undefined function checkUndefined(array) { return array.includes(undefined); } // Create an array const arr1 = ["Learn", "Share", "IT"]; console.log(checkUndefined(arr1)); const arr2 = ["Learn", "Share", "IT", undefined]; console.log(checkUndefined(arr2)); const arr3 = ["Learn", , "Share", "IT"]; console.log(checkUndefined(arr3));
Output:
false
true
true
Summary
This article shared two solutions to check if an array contains undefined in JavaScript. If your program is required to treat empty elements as undefined, you need to use the includes()
method because the some()
method does not work for empty elements. 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