array includes in typescript

To check whether an array contains a specific element in TypeScript, we can use the .includes() method. Here's an example:

index.ts
const myArray: string[] = ['apple', 'banana', 'orange'];

if (myArray.includes('banana')) {
  console.log('My array includes banana');
} else {
  console.log('My array does not include banana');
}
197 chars
8 lines

In this example, we create an array of strings and use the .includes() method to check if it includes the string 'banana'. If it does, we log a message accordingly.

Note that the .includes() method returns a boolean value, which makes it ideal for conditional statements.

gistlibby LogSnag