Last updated: Mar 1, 2024
Reading timeยท4 min
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.
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'); }
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.
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.
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
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.
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.
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'); }
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.
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
.
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
.
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
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.
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.
Alternatively, you can use the indexOf()
method.
If the indexOf()
method doesn't return -1
, the value is contained in the
array.
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
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
.
You can also use the hasOwnProperty method to check if an index exists.
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.
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.
const arr = ['bobby', 'hadz', 'com']; console.log(arr.hasOwnProperty(0)); // ๐๏ธ true console.log(arr.hasOwnProperty(100)); // ๐๏ธ false
try/catch
You can also use a try/catch
statement to check if an index exists in a nested
array.
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'); }
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.