Multiple choice technology web technology

What is the output for the below lines of code? var main_string = "JavaScript is the best Web Programming language"; var sub_string = "Script"; alert(main_string.indexOf(sub_string));

  1. -1

  2. 4

  3. JavaScript error in the browser

  4. 5

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

The indexOf method returns the position of the first occurrence of a substring. In 'JavaScript is the best', counting starts from 0, so 'J' is at position 0 and 'S' is at position 4. The substring 'Script' starts at index 4.