[JavaScript] How to find the index of given element in an array?
I want to find the position of a given element in an array, how can I do that in Javascript?
1 Answer
4 years ago by Jahaan
indexOf() array method is used to find the index of a given element in Javascript
Syntax
Array.indexOf(searchElement, fromIndex)
Example
let str = 'Hello World';
console.log(str.indexOf('r')); // Outputs 8
console.log(str.indexOf('k')); //Outputs -1 as k is not present in the given string
Run the above example online here: https://onecompiler.com/javascript/3xn76e29z
4 years ago by Meera