JS: String Index

By Xah Lee. Date: .

Index of String

Many string methods take index (integer) as argument, or return a index.

Some string methods also allow negative index. Negative index usually means count from right to left, starting at the end of string, but some method treat negative index as 0.

  a   b   c   d   e   f   g
0   1   2   3   ...     -1

The most intuitive way to understand index is to think of index as between the chars. Index 0 is before first character. Index 1 is after first character. Index -1 is the place between last character and second last character.