# The String indexOf() method

> Learn how the JavaScript indexOf() method returns the position of the first occurrence of a substring, or -1 when it is not found, plus an optional start index.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2019-02-20 | Topics: [JavaScript](https://flaviocopes.com/tags/js/) | Canonical: https://flaviocopes.com/javascript-string-indexof/

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

```js
'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:

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