Check if an Array contains Undefined in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
4 min

banner

# Table of Contents

  1. Check if an Array contains Undefined in JavaScript
  2. Check if an Array contains Undefined using Array.some()
  3. Checking for undefined values that are not empty elements
  4. Check if an Array contains Empty Elements in JavaScript
  5. Checking only for empty elements, not undefined
  6. Checking only for empty elements, not undefined using indexOf

# Check if an Array contains Undefined in JavaScript

Use the Array.includes() method to check if an array contains undefined values.

The includes method will return true if the array contains at least one undefined value and false otherwise.

index.js
function containsUndefined(arr) { return arr.includes(undefined); } console.log(containsUndefined([1, undefined])); // ๐Ÿ‘‰๏ธ true console.log(containsUndefined([1, 2])); // ๐Ÿ‘‰๏ธ false

check if array contains undefined

The code for this article is available on GitHub

The function takes an array and returns true if the array contains at least one undefined value.

The Array.includes() method takes a value and checks for its existence in the array.

index.js
// ๐Ÿ‘‡๏ธ true console.log([1, undefined, 3].includes(undefined)); // ๐Ÿ‘‡๏ธ false console.log([1, 2, 3].includes(undefined));
If the value is contained in the array, the method returns true, otherwise, false is returned.

Note that this approach would also work if there is an empty element in the array.

index.js
function containsUndefined(arr) { return arr.includes(undefined); } console.log(containsUndefined([, 2])); // ๐Ÿ‘‰๏ธ true console.log(containsUndefined([])); // ๐Ÿ‘‰๏ธ false

We skipped the first element in the array using a comma, however, it still counts as an undefined value.

An alternative approach is to use the Array.some() method.

# Check if an Array contains Undefined using Array.some()

This is a three-step process:

  1. Use the Array.some() method to iterate over the array.
  2. Compare each array element to undefined and return the result.
  3. If the array contains at least one undefined value, the some() method will return true.
index.js
function containsUndefined(arr) { return arr.some(element => element === undefined); } console.log(containsUndefined([1, undefined])); // ๐Ÿ‘‰๏ธ true console.log(containsUndefined([1, 2])); // ๐Ÿ‘‰๏ธ false

check if array contains undefined using array some

The code for this article is available on GitHub

The function we passed to the Array.some() method gets called with each element of the array.

If at least one invocation of the callback function returns a truthy value, the some() method returns true, otherwise, false is returned.

On each iteration, we check if the current array element is equal to undefined using the strict equality (===) operator.

If the condition is met at least once, the some() method will return true.

This approach also works if the array contains empty elements.

index.js
function containsUndefined(arr) { return arr.some(element => element === undefined); } console.log(containsUndefined([, undefined])); // ๐Ÿ‘‰๏ธ true console.log(containsUndefined([])); // ๐Ÿ‘‰๏ธ false
Note that calling the some() method on an empty array returns false for any condition.

# Checking for undefined values that are not empty elements

If you need to check if the array contains undefined values that are not empty elements, use the indexOf() method.

index.js
const arr = [1, undefined, 3]; if (arr.indexOf(undefined) > -1) { // ๐Ÿ‘‡๏ธ this runs console.log('The array contains undefined values'); } else { console.log("The array doesn't contain undefined values"); }

checking for undefined values that are not empty elements

The code for this article is available on GitHub

The if block will only run if the array contains undefined values that are not empty elements.

The Array.includes() method returns true if the array contains empty elements or undefined values (or both).

index.js
const arr = [1, , 3]; console.log(arr.includes(undefined)); // ๐Ÿ‘‰๏ธ true

The array doesn't contain any undefined values but has an empty element, so the includes() method returns true.

# Check if an Array contains Empty Elements in JavaScript

Use the Array.includes() method to check if an array contains empty elements.

The Array.includes() method will return true if the array contains empty elements and false otherwise.

index.js
const arr = [1, , 3]; console.log(arr[1]); // ๐Ÿ‘‰๏ธ undefined console.log(arr.includes(undefined)); // ๐Ÿ‘‰๏ธ true if (arr.includes(undefined)) { console.log(`โœ… array contains an empty element(s) or undefined value(s)`); } else { console.log(`โ›”๏ธ array DOESN'T contain an empty element or undefined value`); }

check if array contains empty elements

The code for this article is available on GitHub

We have an empty element in the array at index 1. If we try to access the element, we get a value of undefined back.

index.js
const arr = [1, , 3]; console.log(arr[1]); // ๐Ÿ‘‰๏ธ undefined

We used the Array.includes() method to check if the array contains undefined values.

Note that this check would also pass if the array contains an element with a value of undefined.

index.js
const arr = [1, undefined , 3]; console.log(arr.includes(undefined)); // ๐Ÿ‘‰๏ธ true

# Checking only for empty elements, not undefined

If you need to make sure the array contains an empty element and not undefined values, use this approach instead.

index.js
function containsEmptyElements(arr) { return Object.values(arr).length !== arr.length; } // ๐Ÿ‘‡๏ธ true console.log(containsEmptyElements([1, 2, , 4])); // ๐Ÿ‘‡๏ธ false console.log(containsEmptyElements([1, 2, undefined, 4]));
The code for this article is available on GitHub

We passed the array to the Object.values() method.

index.js
// [ 1, 2, 4 ] console.log(Object.values([1, 2, , 4])); // [ 1, 2, undefined, 4 ] console.log(Object.values([1, 2, undefined, 4]));

Notice that empty elements get removed in the call to Object.values() but undefined values don't.

If the length of the array the Object.values() method returns is not equal to the length of the array, then the array contains empty elements.

# Checking only for empty elements, not undefined using indexOf

You can also use the Array.indexOf() method to check if the array contains empty elements.

index.js
function containsEmptyElements(arr) { return arr.indexOf(undefined) === -1 && arr.includes(undefined); } const arr = [1, 2, , 4]; console.log(containsEmptyElements(arr)); // ๐Ÿ‘‰๏ธ true if (containsEmptyElements(arr)) { // ๐Ÿ‘‡๏ธ this runs console.log('โœ… The array contains empty element(s)'); } else { console.log("โ›”๏ธ The array DOESN'T contain empty elements"); }
The code for this article is available on GitHub

We used the logical AND (&&) operator to chain multiple conditions.

The if statement checks if the Array.indexOf() method returns -1 and the array contains undefined values.

This check would only succeed if the array has one or more empty elements.

The Array.indexOf() method returns -1 only when it doesn't find the supplied value.

The includes() method treats empty array elements as undefined, however, empty elements don't actually have a value of undefined.

This is why the indexOf method doesn't find an element with the value of undefined in the array.

If both of the conditions are met, the array contains one or more empty elements.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev