JavaScript array items can be accessed using its index.
You can check if a value exists at a certain index of an array in JavaScript using the if (myArray.at(indexToCheck) == itemToFind) statement.
Basic Example
var myArray = [5, 12, 8, 130, 44];
var indexToCheck = 1;
var itemToFind = 12;
if (myArray.at(indexToCheck) == itemToFind) {
console.log(itemToFind + " Exists at the index " + indexToCheck);
} else {
console.log(itemToFind + " doesn't exist at the index " + indexToCheck);
}
This tutorial teaches you the different methods to check if a value exists at a certain index of an array in JavaScript and when it is appropriate to use each method.
Using At
The at() method takes an integer value and returns the item at that specific position of the array.
The array index is 0-based.
To check if a value exists at a certain index of an array using the at()
method,
- Get the item at the specific position of the array
- Check if the item is equal to a specific value using the
if
statement. - If the
index
returned is equal to the desired index, then the item exists at the specific index
Use this method when you want to find the item at the specific index and check it against the desired item. Later, you can also use the found item for any other purposes.
Code
The following code checks if item 12
exists at the first
position of the array using the at()
method.
var myArray = [5, 12, 8, 130, 44];
var indexToCheck = 1;
var itemToFind = 12;
if (myArray.at(indexToCheck) == itemToFind) {
console.log(itemToFind + " Exists at the index " + indexToCheck);
} else {
console.log(itemToFind + " doesn't exist at the index " + indexToCheck);
}
Output
12 Exists at the index 1
Using Indexof
The indexOf() method returns the first index of the given item in the array.
The array index is 0-based.
To check if an element exists at a specific position of an array using the indexOf()
method,
- Find the index of the item using the
indexOf()
method - Check if the index is equal to the desired index using the
if
statement.
Use this method when you want to find the index of a specific item and check it against the desired index.
Code
The following code checks if item 12
exists at the first
position of the array using the indexOf()
method.
var myArray = [5, 12, 8, 130, 44];
var indexToCheck = 1;
var itemToFind = 12;
if (myArray.indexOf(itemToFind) == indexToCheck) {
console.log(itemToFind + " Exists at the index " + indexToCheck);
} else {
console.log(itemToFind + " doesn't exist at the index " + indexToCheck);
}
Output
12 Exists at the index 1
Using FindIndex
The findIndex() method returns the first index of the given item in the array that passes a test function.
The array index is 0-based.
To check if an element exists at a specific position of an array using the findIndex()
method,
- Find the item’s index using the
findIndex()
method and a test function. - Check if the index is equal to the desired index using the
if
statement. - If the index is equal to the desired index, then the item exists at a certain array position.
Use this method when you want to find the item’s index using a test function. For example, if a value at a specific array position is greater than X.
Code
The following code checks if the number at index 1
is equal to 12
.
var myArray = [5, 12, 8, 55, 44];
var indexToCheck = 1;
var itemToFind = 12;
var foundIndex = myArray.findIndex(checkNumber);
function checkNumber(number) {
return number == itemToFind;
}
if (foundIndex == indexToCheck) {
console.log(itemToFind + " Exists at the index " + indexToCheck);
} else {
console.log(itemToFind + " doesn't exist at the index " + indexToCheck);
}
Output
12 Exists at the index 1