How to Check if a String Is Present in a TypeScript Array

  1. Use the includes() Method to Check if a String Is Present in a TypeScript Array
  2. Use the indexOf() Method to Check if a String Is Present in a TypeScript Array
  3. Use the find() Method to Check if a String Is Present in a TypeScript Array
  4. Use the some() Method to Check if a String Is Present in a TypeScript Array
How to Check if a String Is Present in a TypeScript Array

This article will demonstrate how to use the various built-in functions present in the array prototype for searching a string in an array.

Use the includes() Method to Check if a String Is Present in a TypeScript Array

The includes() method determines whether a target element is present in the array. It takes in the target element and returns a boolean value to represent the value in the array.

While checking for equality, the algorithm considers all zero values equal, regardless of sign. ( -0, +0 are all considered to be equal to 0), but false is not equal to 0. It uses the sameValueZero algorithm for searching whether an element is present in the array.

The following code segment shows how the includes() method can be used.

var fruitsArray : string[] = ['apple', 'orange', 'lichi', 'banana'];
if(fruitsArray.includes('orange')){
    console.log('orange is present in array');
}

Output:

"orange is present in array"

The includes() method is case-sensitive, so we should be careful while passing values for searching. Another parameter determines from which index in the array to start the search.

The following code segment demonstrates this.

var fruitsArray : string[] = ['apple', 'orange', 'lichi', 'banana'];
if(!fruitsArray.includes('orange', 2)){
    console.log('orange is not present in array after index = 2');
}

Output:

"orange is not present in the array after index = 2"

Use the indexOf() Method to Check if a String Is Present in a TypeScript Array

The indexOf() method behaves similar to the includes() method. Their difference lies in the searching algorithm where it checks for equality by the strict equality operator or ===.

The following code segment shows how to find all the occurrences of an element in an array.

var fruitsArray : string[] = ['apple', 'orange', 'lichi', 'orange', 'banana'];

var targetFruit : string = 'orange';
var foundIndices : number[] = [];
let idx : number = fruitsArray.indexOf(targetFruit);
while(idx !== -1){
    foundIndices.push(idx);
    idx = fruitsArray.indexOf(targetFruit, idx + 1);
}
console.log(foundIndices);

Output:

[1, 3] 

Both of the indexOf() and includes() methods are similar. However, checking for the presence of NaN fails in the case of indexOf() while includes() successfully determines its presence.

Another difference is the return type of the two methods, the index of the first occurrence of the element and the boolean value, respectively.

Consider the code below.

var numbers : number[] = [ 1, 2, NaN, 3];

console.log(numbers.includes(NaN));
console.log(numbers.indexOf(NaN));

Output:

true
-1

Use the find() Method to Check if a String Is Present in a TypeScript Array

The find() method returns the first occurrence of the target element if it successfully executes, or else it returns undefined.

var fruitsArray : string[] = ['apple', 'orange', 'lichi', 'banana'];
if(fruitsArray.find(e => e === 'orange')){
    console.log('orange is present in array');
}

Output:

"orange is present in array"

Use the some() Method to Check if a String Is Present in a TypeScript Array

The some() method checks if at least one element in the array passes the predicated function passed to it. The following code segment demonstrates how it can search for a string in an array.

var fruitsArray : string[] = ['apple', 'orange', 'lichi', 'banana'];
const search = (targetElement : string) => (arrElement : string) => arrElement === targetElement;
console.log(fruitsArray.some(search('orange')));
console.log(fruitsArray.some(search('grapes')));

Output:

true
false
Shuvayan Ghosh Dastidar avatar Shuvayan Ghosh Dastidar avatar

Shuvayan is a professional software developer with an avid interest in all kinds of technology and programming languages. He loves all kinds of problem solving and writing about his experiences.

LinkedIn Website

Related Article - TypeScript Array