2

I just ran into a bit of confusion today, "string".indexOf(''); always returns 0, but I would expect -1 (for false); inversely, "string".lastIndexOf(''); always returns 6

lastIndexOf is easier to understand, since string is 6 letters long ("string".length, being zero-indexed returns 5) but I don't see anywhere in the ECMAscript spec (5.1 or 6.0) that describes why '' would be treated like a word/character boundary

What, exactly, is going on here?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
JKirchartz
  • 17,612
  • 7
  • 60
  • 88
  • Yes, `''` is treated like a word/character boundary. – PitaJ Oct 14 '15 at 18:31
  • `indexOf()` returns the _first_ occurrence of a found match and returns `-1` _only_ if the string isn't found at all. – Etheryte Oct 14 '15 at 18:32
  • `''` is treated like...ummm...an empty string. The first occurrence of an empty string inside another string is right at its start. –  Oct 14 '15 at 18:38
  • 2
    The "duplicate" is about Java. And this question asks for a clarification of the spec, not for a vague reason. So I vote to reopen and add [language-lawyer] tag. – Oriol Oct 14 '15 at 18:42

1 Answers1

6

The spec says:

Return the smallest possible integer k not smaller than start such that k+searchLen is not greater than len, and for all nonnegative integers j less than searchLen, the character at position k+j of S is the same as the character at position j of searchStr; but if there is no such integer k, then return the value -1.

That condition is fulfilled at position 0 because of vacuous truth: since you are searching the empty string, any statement you can think of will hold for every character, because it has no characters.

More formally, for any statement P, if S = ∅, P(x) holds ∀ x ∈ S.

Oriol
  • 274,082
  • 63
  • 437
  • 513