JavaScript program to find the index of a particular character in a given string


Following program shows you how to find the index of a particular character in a given string.

var input = prompt("Please enter a string:");
var character = prompt("Please enter a character:") ;
console.log("Index of given character is: " + input.indexOf(character));

Output:

Example1:

Please enter a string:
hello
Please enter a character:
o
Index of given character is: 4

Example2:

Please enter a string:
hello
Please enter a character:
r
Index of given character is: -1