JS Built-ins
StringString.prototype.indexOfbeginner

String.prototype.indexOf() β€” empty string always at 0

πŸ“– Usage Example
const str = 'hello world'
str.indexOf('world')    // β†’ 6
str.indexOf('xyz')      // β†’ -1
str.indexOf('l')        // β†’ 2

Use indexOf() to find the index of the first occurrence of a substring, returning -1 if not found.

String.prototype.indexOf() β€” Empty String

str.indexOf('') always returns 0 regardless of the string content.

Challenge: Implement emptyIndex(str) that returns str.indexOf('').

emptyIndex('hello') // β†’ 0
emptyIndex('')       // β†’ 0
πŸ’‘ Hints (1)
  • β€’ The empty string is found at position 0 in every string.