How to use JavaScript String indexOf()

String indexOf

On this page

String indexOf() method

Program to use JavaScript String indexOf() method

let a = "JavaScript best programing language";
    
    let b = a.indexOf("best");
    
    console.log(b);
    

Output

11
    

Steps Description

  • Declare two variable a and b
  • Initialize two variable with value of string a = “Hello World in JavaScript” and b
  • String .length Calculates the length of a string by counting its characters and output it as a number.
  • The value of b variable is displayed with the help of console.log inbuilt function