EN
JavaScript - find character index in string
3
points
In this article, we would like to show you how to find a character in a string in JavaScript.
Quick solution:
'ABC'.indexOf('B') // 1
Practical examples
To find the character in a string we need to use indexOf()
method.
// ONLINE-RUNNER:browser;
const result = 'dirask is awesome!'.indexOf('d');
console.log(result); //0
// ONLINE-RUNNER:browser;
const myString = 'dirask is awesome!';
console.log(myString.indexOf('i')); //1
Note:
If there is more than one occurrence of the character, the
infexOf()
method returns the position of the first one, starting from the left.