Layout Options

  • Fixed Header
    Makes the header top fixed, always visible!
  • Fixed Sidebar
    Makes the sidebar left fixed, always visible!
Array IndexOf | lastIndexOf

Description

The indexOf() method is used to returns the first index at which a given element can be found in the array, or -1 if it is not found. The array is searched forwards (front to back).

This method has the form:

Array.indexOf(searchElement[, fromIndex])
Parameter Description
searchElement (required) Specifies the element to be located in the array
fromIndex (optional) Specifies the index at which to begin the search (default = 0)

If the fromIndex is greater than the length of the array, -1 is returned
If the fromIndex is less than zero, the search begins at the offset from the end of the array

Note: In all cases, the array is searched from front to back


Return Value

A Number that indicates the position of searchElement, or -1 if not found

Examples

The following example shows the basic use of this method.

<script>
var cols = [ "Cyan", "Magenta", "Yellow", "Black" ];
document.write(cols.indexOf("Magenta"));
</script>

This produces the following result:

Browser Support

Firefox IE Chrome Opera Safari

Version Information

This form was added in version: 1.6

Description

The lastIndexOf() method is used to returns the last index at which a given element can be found in the array, or -1 if it is not found. The array is searched backwards (back to front).

This method has the form:

Array.lastIndexOf(searchElement[, fromIndex])
Parameter Description
searchElement (required) Specifies the element to be located in the array
fromIndex (optional) Specifies the index at which to start searching backwards (default = array length)

If the fromIndex is greater than the length of the array, the entire array is searched
If the fromIndex is less than zero, the search begins at the offset from the end of the array

Note: In all cases, the array is searched from back to front


Examples

The following example shows the basic use of this method.

<script>
var cols = [ "Cyan", "Magenta", "Yellow", "Black" ];
document.write(cols.lastIndexOf("Magenta"));
</script>

This produces the following result:

Browser Support

Firefox IE Chrome Opera Safari

Version Information

This form was added in version: 1.6