Latest web development tutorials

JavaScript lastIndexOf () method

Array Object Reference JavaScript Array Object

Examples

Find the location of array elements "Apple" appears:

var fruits = [ "Banana", "Orange", "Apple", "Mango"];
var a = fruits.lastIndexOf ( "Apple");

a output:

2

Examples of the above output means that "Apple" is located in the array in the first two positions.

try it"

Definition and Usage

lastIndexOf () method returns the position of a specified string value last appeared in a string at a specified location from the search forward.

If you want to retrieve the string value is not present, the method returns -1.

This method is retrieved from the head to the tail string stringObject, to see if it contains a substring searchvalue. Start retrieving location (not specified fromindex time) at the end of the fromindex string or strings. If you find a searchvalue, the first character position in stringObject searchvalue in return. stringObject character position is zero-based.

Tip: If you want to find the first occurrence of the string, use the indexOf () method .


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support lastIndexOf () property, but in Internet Explorer 8 and earlier versions of IE does not support this method.


grammar

array .lastIndexOf (item, start)

Parameter Values

parameter description
item Required. Provisions need to retrieve a string value.
start The optional integer parameter. Specified in the string to begin the search location. Its legal value of between 0 stringObject.length - 1. If the argument is omitted, it will start retrieving from the last character of the string.

return value

Type description
Number If there searchvalue before stringObject in fromindex position, it returns the position of appearing last searchvalue.

technical details

JavaScript version: 1.6


More examples

Examples

Find an array "Apple" here:

var fruits=["Banana","Orange","Apple","Mango","Banana","Orange","Apple"];
var a = fruits.lastIndexOf("Apple");

a output:

6

try it"

Examples

Find the string "Apple" appears from the fourth position in the array:

var fruits=["Banana","Orange","Apple","Mango","Banana","Orange","Apple"];
var a = fruits.lastIndexOf("Apple",4);

a result output:

2

try it"

Array Object Reference JavaScript Array Object