Skip to content

Latest commit

 

History

History
90 lines (68 loc) · 2.86 KB

File metadata and controls

90 lines (68 loc) · 2.86 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
indexOf Method (Array) (JavaScript) | Microsoft Docs
01/18/2017
windows-client-threshold
devlang-javascript
language-reference
JavaScript
TypeScript
DHTML
arrays [JavaScript], indexOf method
indexOf method [JavaScript]
5bee31ae-aaf1-4466-8cfd-ed287e3cdf17
12
mikejo5000
mikejo
ghogen

indexOf Method (Array) (JavaScript)

Returns the index of the first occurrence of a value in an array.

Syntax

  
array1.indexOf(searchElement[, fromIndex])  

Parameters

Parameter Definition
array1 Required. An array object.
searchElement Required. The value to locate in array1.
fromIndex Optional. The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.

Return Value

The index of the first occurrence of searchElement in the array, or -1 if searchElement is not found.

Remarks

The indexOf method searches an array for a specified value. The method returns the index of the first occurrence, or -1 if the specified value is not found.

The search occurs in ascending index order.

The array elements are compared to the searchElement value by strict equality, similar to the === operator. For more information, see Comparison Operators.

The optional fromIndex argument specifies the array index at which to begin the search. If fromIndex is greater than or equal to the array length, -1 is returned. If fromIndex is negative, the search starts at the array length plus fromIndex.

Example

The following examples illustrate the use of the indexOf method.

// Create an array. (The elements start at index 0.)  
var ar = ["ab", "cd", "ef", "ab", "cd"];  
  
// Determine the first location of "cd".  
document.write(ar.indexOf("cd") + "<br/>");  
  
// Output: 1  
  
// Find "cd" starting at index 2.  
document.write(ar.indexOf("cd", 2) + "<br/>");  
  
// Output: 4  
  
// Find "gh" (which is not found).  
document.write (ar.indexOf("gh")+ "<br/>");  
  
// Output: -1  
  
// Find "ab" with a fromIndex argument of -2.  
// The search starts at index 3, which is the array length plus -2.  
document.write (ar.indexOf("ab", -2) + "<br/>");  
// Output: 3  
  

Requirements

[!INCLUDEjsv9]

See Also

JavaScript Methods
Array Object
Using Arrays