TypeScript - Array indexOf()


Advertisements

indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

Syntax

array.indexOf(searchElement[, fromIndex]);

Parameter Details

  • searchElement − Element to locate in the array.

  • fromIndex − The index at which to begin the search. Defaults to 0, i.e. the whole array will be searched. If the index is greater than or equal to the length of the array, -1 is returned.

Return Value

Returns the index of the found element.

Example

var index = [12, 5, 8, 130, 44].indexOf(8); 
console.log("index is : " + index );

On compiling, it will generate the same code in JavaScript.

Its output is as follows −

index is : 2
typescript_arrays.htm

Useful Video Courses


Video

Angular Essentials - Admin App, Typescript, Docker, c3.js

45 Lectures 4 hours

Antonio Papa

Video

NestJs: Modern ways to build APIs with Typescript and NestJs

41 Lectures 7 hours

Haider Malik

Video

Master Typescript : Learn Typescript from scratch

60 Lectures 2.5 hours

Skillbakerystudios

Video

Three.js and TypeScript

77 Lectures 8 hours

Sean Bradley

Video

TypeScript from Scratch: Go from zero to hero in TypeScript

77 Lectures 3.5 hours

TELCOMA Global

Video

Advanced TypeScript: Generic Search, Sorting and Filtering

19 Lectures 3 hours

Christopher Frewin

Advertisements