Learning JavaScript? Download my free JavaScript Handbook 🔥

Gives the position of the first occurrence of the string str in the current string. Returns -1 if the string is not found.

'JavaScript'.indexOf('Script') //4
'JavaScript'.indexOf('JavaScript') //0
'JavaScript'.indexOf('aSc') //3
'JavaScript'.indexOf('C++') //-1

You can pass a second parameters to set the starting point:

'a nice string'.indexOf('nice') !== -1 //true
'a nice string'.indexOf('nice', 3) !== -1 //false
'a nice string'.indexOf('nice', 2) !== -1 //true

Edit this page

Here's my latest YouTube video. I talk about why I think that dogs are a great help for developers working remotely: