Easy Tutorial
❮ Prop Email Form Jsref Var ❯

JavaScript indexOf() Method

JavaScript String Object

Example

Find the string "welcome":

n Output result:


Definition and Usage

The indexOf() method returns the position of the first occurrence of a specified string value in a string.

If no matching string is found, it returns -1.

Note: The indexOf() method is case-sensitive.

Tip: You can also look at the similar method lastIndexOf().


Browser Support

All major browsers support the indexOf() method.


Syntax

Parameter Values

Parameter Description
searchvalue Required. The string value to search for.
start Optional integer parameter. Specifies the position in the string to start the search from. Its valid range is from 0 to string Object.length - 1. If omitted, the search starts from the first character of the string.

Return Value

Type Description
Number The position of the first occurrence of the specified string, or -1 if it is not found.

Technical Details

| JavaScript Version: | 1.0 | | --- | --- |


More Examples

Example

Find the first occurrence of the character "e" in the string:

n Output result:

Example

Find the first occurrence of the character "e" starting from the fifth position in the string:

n Output result:


JavaScript String Object

❮ Prop Email Form Jsref Var ❯