Examples
Search a string for "welcome":
let text = "Hello world, welcome to the universe.";
let result = text.indexOf["welcome"];
Try it Yourself »
Search a string for "Welcome":
let text = "Hello world, welcome to the universe.";
let result = text.indexOf["Welcome"];
Try it Yourself »
Find the first occurrence of "e":
let text = "Hello world, welcome to the universe.";
text.indexOf["e"];
Try it Yourself »
Find the first occurrence of "e", starting at position 5:
let text = "Hello world, welcome to the universe.";
text.indexOf["e", 5];
Try it Yourself »
Find the first occurrence of "a":
let text = "Hello world, welcome to the universe.";
text.indexOf["a"];
Try it Yourself »
Definition and Usage
The indexOf[]
method returns the position of the first
occurrence of a value in a string.
The indexOf[]
method returns -1 if the value is not found.
The indexOf[]
method is case sensitive.
Syntax
string.indexOf[searchvalue, start]
Parameters
Parameter | Description |
searchvalue | Required. The string to search for. |
start | Optional. The position to start from [default is 0]. |
Return Value
Type | Description |
A number | The first position where the search-value occurs. -1 if it never occurs. |
The Differense Between
String indexOf[] and String search[]
The indexOf[]
method cannot search against a regular expression.
The search[]
cannot take a start position argument.
Browser Support
indexOf[]
is an ECMAScript1 [ES1] feature.
ES1 [JavaScript 1997] is fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |
Examples
Find the first index of "Apple":
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let index = fruits.indexOf["Apple"];
Try it Yourself »
Start at index 3:
const fruits = ["Banana", "Orange", "Apple", "Mango", "Apple"];
let index = fruits.indexOf["Apple", 3];
Try it Yourself »
More examples below.
Definition and Usage
The indexOf[]
method returns the first index [position] of a specified value.
The indexOf[]
method returns -1 if the value is not found.
The indexOf[]
method starts at a specified index and searches from left to right.
By default the search starts at the first element and ends at the last.
Negative start values counts from the last element [but still searches from left to right].
Syntax
array.indexOf[item, start]
Parameters
Parameter | Description | |
item | Required. The value to search for. | |
start | Optional. Where to start the search. Default value is 0. Negative values start the search from the end of the array. |
Return Value
Type | Description |
A number | The index [position] of the first item found. -1 if the item is not found. |
Note
In an array, the first element has index [position] 0, the second has index 1, ...
More Examples
Find the first index of "Apple", starting from the last element:
const fruits = ["Banana", "Orange", "Apple", "Mango", "Apple"];
let index = fruits.indexOf["Apple", -1];
Try it Yourself »
Browser Support
indexOf[]
is an ECMAScript5 [ES5] feature.
ES5 [JavaScript 2009] fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |
The JavaScript array indexOf[] method is used to search the specified element in the given array and returns the index of the first match. Syntax: Parameters: Returns: Example:array.indexOf [element,index]
element: It is required and represents the element which have to be searched.
index: It is optional and
represent the index position from where search starts.
Index of the first match for a specific element.DOCTYPE html>
var a = ["GOLD","SILVER","DIAMOND","RUBY","PLATINUM"]
var result=a.indexOf["DIAMOND"];
document.writeln[result];
Output: