Learning JavaScript? Download my free JavaScript Handbook 🔥

Return the position of the first occurrence of the string str in the current string.

It returns the index of the start of the occurrence, or -1 if no occurrence is found.

'JavaScript'.search('Script') //4
'JavaScript'.search('TypeScript') //-1

You can search using a regular expression (and in reality, even if you pass a string, that’s internally and transparently used as a regular expression too).

'JavaScript'.search(/Script/) //4
'JavaScript'.search(/script/i) //4
'JavaScript'.search(/a+v/) //1


Follow my new project: From Zero to App

A video-based learning project where I create a React + GraphQL app from idea to deployment! 9 free videos already out!