VOOZH about

URL: https://www.geeksforgeeks.org/typescript/typescript-array-lastindexof-method/

⇱ TypeScript Array lastIndexOf() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

TypeScript Array lastIndexOf() Method

Last Updated : 19 Jul, 2024

The Array.prototype.lastIndexOf() method in TypeScript is used to find the last index at which a given element can be found in the array, searching backwards from the fromIndex. It returns the index of the found element, or -1 if the element is not present.

Syntax:

array.lastIndexOf(searchElement[, fromIndex])

Parameter: This method accepts two parameter as mentioned above and described below: 

  • searchElement : This parameter is the element to locate in the array.
  • fromIndex : This parameter is the index at which to start searching backwards.

Return Value: This method returns the index of the found element from the last. 

Below examples illustrate the Array lastIndexOf() method in TypeScript.

Examples of Array lastIndexOf() Method

Example 1: Basic Usage of lastIndexOf()

In this example, we will demonstrate the basic usage of the lastIndexOf() method to find the last occurrence of an element in an array.

Output:

3

Example 2:

Output:

3
1 2
Comment
Article Tags:
Article Tags:

Explore