How to useArray.includes() method in Typescript
The includes
method is a straightforward way to check if an array contains a specific value, including strings.
Syntax:
const arrayContainsString = myArray.includes("yourString");
Example: In this example, we are using Array.include() method.
const myArray: string[] = ["apple", "banana",
"orange"];
if (myArray.includes("banana")) {
console.log(`Array contains the
string 'banana'.`);
} else {
console.log(`Array does not contain
the string 'banana'.`);
}
Output:
Array contains the string 'banana'.
How to Check Whether an Array Contains a String in TypeScript ?
To check whether an array contains a string in typescript we have a different approach. In this article, we are going to learn how to check whether an array contains a string in typescript.
Below are the approaches to check whether an array contains a string in typescript:
Table of Content
- Approach 1: Using Array.includes() method
- Approach 2: Using Array.indexOf() method
- Approach 3: Using Array.some() method
- Approach 4: Using Array.find() method
- Approach 5: Using Array.find() method with a boolean condition
Contact Us