TypeScript Array indexOf()

In this chapter, we will explore the indexOf() method for arrays in TypeScript. This method is a built-in function that helps in finding the index of the first occurrence of a specified element within an array. Understanding how to use indexOf() is useful for locating elements within arrays.

Table of Contents

  1. Definition
  2. Syntax
  3. Examples
  4. Conclusion

1. Definition

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

2. Syntax

array.indexOf(searchElement, fromIndex?);

Parameters

  • searchElement: The element to locate in the array.
  • fromIndex (optional): The index to start the search at. If the index is greater than or equal to the array's length, -1 is returned, meaning the array will not be searched. Defaults to 0. If the index is negative, it is taken as the offset from the end of the array. Note: if the calculated index is less than 0, the whole array will be searched.

Return Value

The method returns the first index at which the element can be found in the array; otherwise, it returns -1.

3. Examples

Let's look at some examples to understand how indexOf() works in TypeScript.

Example 1: Basic Usage

In this example, we find the index of a specific number in the array.

let numbers: number[] = [1, 2, 3, 4, 5];
let result = numbers.indexOf(3);
console.log(result); // Output: 2

Example 2: Using fromIndex

In this example, we use the fromIndex parameter to start the search from a specific index.

let numbers: number[] = [1, 2, 3, 4, 5];
let result = numbers.indexOf(3, 3);
console.log(result); // Output: -1

Example 3: Checking for Strings

In this example, we find the index of a specific string in the array.

let fruits: string[] = ["apple", "banana", "mango"];
let result = fruits.indexOf("banana");
console.log(result); // Output: 1

Example 4: Case Sensitivity

The indexOf() method is case-sensitive. In this example, we see how case sensitivity affects the result.

let fruits: string[] = ["apple", "banana", "mango"];
let result = fruits.indexOf("Banana");
console.log(result); // Output: -1

Example 5: Checking for NaN

In this example, we check if an array includes NaN. The indexOf() method cannot correctly find NaN, unlike the includes() method.

let numbers: number[] = [1, 2, NaN, 4, 5];
let result = numbers.indexOf(NaN);
console.log(result); // Output: -1

Example 6: Element Not Found

In this example, we use indexOf() with an element that is not present in the array, resulting in -1.

let numbers: number[] = [1, 3, 5, 7];
let result = numbers.indexOf(10);
console.log(result); // Output: -1

4. Conclusion

In this chapter, we explored the indexOf() method for arrays in TypeScript, which is used to find the index of the first occurrence of a specified element within an array. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use indexOf() effectively can help in various array manipulation tasks in TypeScript, especially when locating the position of elements within arrays.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare