TypeScript allows us to do more with JavaScript. It gives us additional syntax, better error handling and lots more. We can get the last index of an element in an array in TypeScript using the lastIndexOf()
method. This method returns the index of the last occurrence of an element in an array.
array.lastIndexOf(element)
element
: The element we want to find the last occurrence of in the array.
An integer value representing the index of the last occurrence of the given element in the array.
// create arrays in TypeScriptlet names: string[] = ["Theodore", "James", "Peter", "Amaka"]let numbers : Array<number>;numbers = [12, 34, 5, 12, 0.9]let cars : Array<string> = ["Porsche", "Toyota", "Lexus", "Toyota"]// get last index of some elementsconsole.log(names.lastIndexOf("Theodore")) // 0console.log(numbers.lastIndexOf(12)) // 3console.log(cars.lastIndexOf("Toyota")) // 3