How to Get the Index of a Value in an Array in JavaScript
In this lesson, we will see how to get the index of a value in an array in JavaScript, sometimes we want to get the index of an element in an array in JavaScript so how can we do that?
The indexOf() method
To do that we can use the indexOf() method that returns the index of the element if exists and -1 if not.
const colors = ["blue", "orange", "black", "red"];
console.log(colors.indexOf("red"));
//3
console.log(colors.indexOf("white"));
//-1