TypeScript Array includes() Method
Last Updated :
29 Jan, 2024
TypeScript includes() method allows you to determine if a specific element is present, within an array.
can also determine if a particular element exists or not at a particular index position.
Syntax:
array.includes(searchElement[, fromIndex])
Parameters:
- searchElement: The value to search for within the array.
- fromIndex (optional): The starting index at which to begin searching.
Return Value:
- It will return true if the array contains the search element, and false otherwise.
Example 1: In this example, we will check whether a given array contains a particular element.
Javascript
const fruits: string[] =
[ "apple" , "banana" , "orange" ];
const hasApple: boolean =
fruits.includes( "apple" );
console.log(hasApple);
|
Output:
true
Example 2: In this example, we will check whether a number exists or not at a given particular index position from starting.
Javascript
const numbers: number[] = [1, 2, 3, 1];
const startIndex: number = 2;
const foundSecond1: boolean =
numbers.slice(startIndex).includes(1);
console.log(foundSecond1);
|
Output:
true
Share your thoughts in the comments
Please Login to comment...