Open In App

How to Check Whether an Array Contains a String in TypeScript ?

Last Updated : 07 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Approach 1: Using Array.includes() method

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.

Javascript
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'.

Approach 2: Using Array.indexOf() method

The indexOf method returns the index of the first occurrence of a specified element in an array. If the element is not present, it returns -1.

Syntax:

const index = myArray.indexOf("yourString");
const arrayContainsString = index !== -1;

Example: In this example, we are using Array.indexof() method.

Javascript
const myArray: string[] = ["apple",
    "banana", "orange"];
const searchString: string = "banana";

if (myArray.indexOf(searchString) !== -1) {
    console.log(`Array contains the
    string '${searchString}'.`);
} else {
    console.log(`Array does not contain
    the string '${searchString}'.`);
}

Output:

Array contains the string 'banana'.

Approach 3: Using Array.some() method

The some method tests whether at least one element in the array passes the provided function.

Syntax:

const arrayContainsString = myArray.some(item => item === "yourString");

Example: In this example, we are using Array.some() method.

Javascript
const myArray: string[] = ["apple",
    "banana", "orange"];
const searchString: string = "banana";

if (myArray.some(item => item === searchString)) {
    console.log(`Array contains 
    the string '${searchString}'.`);
} else {
    console.log(`Array does not 
    contain the string '${searchString}'.`);
}

Output:

Array contains the string 'banana'.

Approach 4: Using Array.find() method

The find() method returns the value of the first element in the provided array that satisfies the provided testing function. Otherwise, it returns undefined.

Syntax:

const foundString = myArray.find(item => item === "yourString");
const arrayContainsString = foundString !== undefined;

Example: In this example, we are using Array.find() method.

JavaScript
const myArray: string[] = ["apple", "banana", "orange"];
const searchString: string = "banana";

const foundString = myArray.find(item => item === searchString);

if (foundString) {
    console.log(`Array contains the string '${searchString}'.`);
} else {
    console.log(`Array does not contain the string '${searchString}'.`);
}

Output:

Array contains the string 'banana'.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads