TypeScript Array includes()

In this chapter, we will explore the includes() method for arrays in TypeScript. This method is a built-in function that helps in determining whether an array contains a certain element. Understanding how to use includes() is useful for checking the presence of elements within arrays.

Table of Contents

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

1. Definition

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

2. Syntax

array.includes(valueToFind, fromIndex?);

Parameters

  • valueToFind: The value to search for in the array.
  • fromIndex (optional): The position in the array at which to begin the search. Defaults to 0.

Return Value

The method returns a boolean value: true if the valueToFind is found within the array, otherwise false.

3. Examples

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

Example 1: Basic Usage

In this example, we check if an array includes a specific number.

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

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.includes(3, 3);
console.log(result); // Output: false

Example 3: Checking for Strings

In this example, we check if an array includes a specific string.

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

Example 4: Case Sensitivity

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

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

Example 5: Checking for NaN

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

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

Example 6: Empty Array

In this example, we check the behavior of includes() on an empty array.

let numbers: number[] = [];
let result = numbers.includes(1);
console.log(result); // Output: false

4. Conclusion

In this chapter, we explored the includes() method for arrays in TypeScript, which is used to determine whether an array includes a certain element. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use includes() effectively can help in various array manipulation tasks in TypeScript, especially when checking for the presence 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