This post will discuss how to determine whether an element exists at the specified index in an array in JavaScript.

An array in JavaScript can store elements of any type such as number, string, boolean, bigint, null, undefined, symbol, or an object.

1. Check for undefined value

An undefined value automatically gets assigned in JavaScript, where no value has been explicitly assigned. If you don’t specify all elements in an array literal, an undefined value is substituted for the unspecified elements.

To check for an undefined value at the specified index in the array, you can use the following code:

Download  Run Code

2. Check for nullish value

A nullish value is either null or undefined. To check if a nullish value exists at the specified index in the array, you can use the following code:

Download  Run Code

 
Alternatively, you can use the != operator that filters the nullish value.

Download  Run Code

3. Check for falsy value

There are 7 falsy values in JavaScript – false, zero (0), BigInt (0n), empty string ("", '', ``), null, undefined, and NaN. The following example uses Type Conversion inside an if block to coerce the value present at the specified index to a Boolean and determine whether the value is falsy:

Download  Run Code

That’s all about checking if an element exists at the specified index in an array in JavaScript.