How to check if an Array Index exists in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 1, 2024
4 min

banner

# Table of Contents

  1. Check if an Array Index exists in JavaScript
  2. Check if an Array index exists using includes()
  3. Check if an Array Index exists using the array's length
  4. Check if an Array Index exists using optional chaining
  5. Check if an Array Index exists using indexOf()
  6. Check if an Array Index exists using hasOwnProperty()
  7. Check if an Index exists in a nested array using try/catch

# Check if an Array Index exists in JavaScript

To check if an array index exists, access the array at the specific index and check if the result is not equal to undefined.

If the result is not equal to undefined the array index exists.

index.js
const arr = ['bobby', 'hadz']; if (arr[3] !== undefined) { console.log('The index exists in the array'); } else { // ๐Ÿ‘‡๏ธ this runs console.log('The index does NOT exist'); }

check if array index exists

The code for this article is available on GitHub

JavaScript indexes are zero-based, so the first index in an array is 0 and the last index is equal to array.length - 1.

We access the array at index 3 and check if the result is not equal to undefined.

Since the array only has 2 elements, the last index in the array is 1. Therefore, the condition is never satisfied and the if block doesn't run.

# Check if an Array index exists using includes()

If you need to check if a value is contained in an array, use the includes() method.

The method returns true if the value is contained in the array and false otherwise.

index.js
const arr = ['bobby', 'hadz', 'com']; if (arr.includes('bobby')) { // ๐Ÿ‘‡๏ธ this runs console.log('The value is contained in the array'); } else { console.log('The value is NOT contained in the array'); } console.log(arr.includes('bobby')); // ๐Ÿ‘‰๏ธ true console.log(arr.includes('another')); // ๐Ÿ‘‰๏ธ false

check if array index exists using includes

The code for this article is available on GitHub

The Array.includes() method returns true if the supplied value is contained in the array and false otherwise.

You can also check if an index exists in an array by using the array's length.

# Check if an Array Index exists using the array's length

You can also use the array's length property to check if an array index exists.

If the array has a length greater than N, then index N exists in the array.

index.js
const arr = ['bobby', 'hadz']; if (arr.length > 5) { console.log('Index 5 exists in the array'); } else { // ๐Ÿ‘‡๏ธ this runs console.log('Index 5 does NOT exist in the array'); }

check if array index exists using array length

We check if the length of the array is greater than 5.

If the array has a length greater than 5, then index 5 is guaranteed to exist in the array.

JavaScript indexes are zero-based, so the first element in an array has an index of 0 and the last element has an index of array.length - 1.

If an array has a length of 10, its last index is 10 - 1 = 9.

# Check if an Array Index exists using optional chaining

A newer way to check if an array index exists is to use optional chaining.

The optional chaining (?.) operator will return the array element if the index exists, otherwise, it returns undefined.

index.js
const arr = ['bobby', 'hadz']; const firstIndex = arr?.[1]; console.log(firstIndex); // ๐Ÿ‘‰๏ธ hadz if (firstIndex !== undefined) { // ๐Ÿ‘‰๏ธ index 1 exists in the array } const fifthIndex = arr?.[5]; console.log(fifthIndex); // ๐Ÿ‘‰๏ธ undefined

check if array index exists using optional chaining

The code for this article is available on GitHub

We used the optional chaining operator (?.) to access the array elements at indices 1 and 5.

The optional chaining (?.) operator short-circuits and returns undefined if the value to the left is nullish (null or undefined).

The array element at index 1 exists, so its value gets assigned to the firstIndex variable.

The array element at index 5 doesn't exist, so the optional chaining (?.) operator short-circuits returning undefined.

You can also use the optional chaining operator to check if an index exists in a multi-dimensional array.

index.js
const arr = [ ['a', 'b'], ['c', 'd'], ]; console.log(arr?.[0]); // ๐Ÿ‘‰๏ธ ['a', 'b'] console.log(arr?.[0]?.[1]); // ๐Ÿ‘‰๏ธ 'b' console.log(arr?.[1]?.[2]?.[3]?.[4]); // ๐Ÿ‘‰๏ธ undefined

The arr variable stores a two-dimensional array where the nested arrays contain 2 elements each.

The last index in each nested array is 1.

Trying to access a deeply nested index that doesn't exist doesn't raise an error because we used the optional chaining operator.

# Check if an Array Index exists using indexOf()

Alternatively, you can use the indexOf() method.

If the indexOf() method doesn't return -1, the value is contained in the array.

index.js
const arr = ['bobby', 'hadz']; if (arr.indexOf('bobby') !== -1) { // ๐Ÿ‘‡๏ธ this runs console.log('The value is contained in the array'); } else { console.log('The value is NOT contained in the array'); } console.log(arr.indexOf('bobby')); // ๐Ÿ‘‰๏ธ 0 console.log(arr.indexOf('another')); // ๐Ÿ‘‰๏ธ -1

check if array index exists using indexof

The code for this article is available on GitHub

The Array.indexOf() method returns the index of the first occurrence of the supplied value in the array.

If the value is not contained in the array, the method returns -1.

# Check if an Array Index exists using hasOwnProperty()

You can also use the hasOwnProperty method to check if an index exists.

index.js
const arr = ['bobby', 'hadz', 'com']; if (arr.hasOwnProperty(1)) { console.log(arr[1]); // ๐Ÿ‘‰๏ธ hadz console.log('The index exists in the array') } else { console.log('The specified index does NOT exist'); }

The hasOwnProperty method can be called on most JavaScript objects.

Most commonly the method is used to check if a property exists in an object.

index.js
const obj = {name: 'bobby', age: 30}; console.log(obj.hasOwnProperty('name')); // ๐Ÿ‘‰๏ธ true console.log(obj.hasOwnProperty('another')); // ๐Ÿ‘‰๏ธ false

When the method is called on an array, it returns true if the index exists in the array and false otherwise.

index.js
const arr = ['bobby', 'hadz', 'com']; console.log(arr.hasOwnProperty(0)); // ๐Ÿ‘‰๏ธ true console.log(arr.hasOwnProperty(100)); // ๐Ÿ‘‰๏ธ false

# Check if an Index exists in a nested array using try/catch

You can also use a try/catch statement to check if an index exists in a nested array.

index.js
const arr = [ ['a', 'b'], ['c', 'd'], ]; try { const value1 = arr[0][1]; console.log(value1); // ๐Ÿ‘‰๏ธ b const value2 = arr[0][1][2][3]; console.log(value2); // โ›”๏ธ error } catch (err) { // ๐Ÿ‘‡๏ธ this runs console.log('The index does NOT exist'); }
The code for this article is available on GitHub

We try to access the nested array at specific indices.

If the specified nested index doesn't exist, undefined is returned.

If two consecutive indices don't exist, an error is raised and is then handled by the catch block.

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