Sometimes, we want to get position of the element of the array with JavaScript.
In this article, we’ll look at how to get position of the element of the array with JavaScript.
How to get position of the element of the array with JavaScript?
To get position of the element of the array with JavaScript, we can use the array’s indexOf
method.
For instance, we write:
const pos = [1, 2, 3, 4].indexOf(3);
console.log(pos)
to call indexOf
on [1, 2, 3, 4]
with 3 to get the index of 3 in the array.
Therefore, pos
is 2 since 3 is in the 3rd position in the array.
Conclusion
To get position of the element of the array with JavaScript, we can use the array’s indexOf
method.