I study javascript. In the tutorial, here is an example:

var str = "Widget with id"; alert( str.indexOf("Widget") ); // 0, т.к. "Widget" найден прямо в начале str alert( str.indexOf("id") ); // 1, т.к. "id" найден, начиная с позиции 1 alert( str.indexOf("widget") ); // -1, не найдено, так как поиск учитывает регистр 

Why in 2 cases 1 and not 2 ? This is the 3rd element.

    2 answers 2

    The search is on the line, it means for all the characters: W id get - "found" id in the word Widget.

     W idg et with id 0 1 2 3 ... // разделите строку на символы и увидите сами "Widget with id".split('') 

      Here, as in arrays, everything starts from 0, and the first character is always zero. I found the second example id, starting with the first index, which means it gave 1.