Close
Close full mode
logoWebDevAssist

Check if an element is in an array or not in TypeScript

Check if an element is in an array or not in TypeScript:

We can use all JavaScript array methods in TypeScript. If you want to check if an element is in an array or not, you can use the includes method.

This method can be used in TypeScript as well.

In this post, we will learn how to use includes method to check if an element is in an array or not in TypeScript.

Definition of includes:

includes method is defined as like below:

array.includes(element, index)
  • element is the element to find in the array.
  • index is the starting index to start the search. It is an optional value. It can be either positive or negative. For negative values, the length of the array is added to it to get the start index.

Return value of includes:

It returns one boolean value. true if the element is found in the array and false otherwise.

If the start index is greater or equal to the length of the array, it returns false.

Example of includes:

Let's try one example of includes:

let arr : string[] = new Array('one', 'two', 'three');
console.log(arr.includes('two'));
console.log(arr.includes('four'));

If you run this program, it will give the below output:

true
false

TypeScript array includes
TypeScript array includes

The first log prints true because the string 'two' is in the array arr. The second log prints false because the string 'four' is not in the array.

Start the search from a position:

We can also pass the start index as the second parameter to the index method. It will start the search from that index.

For example:

let arr : string[] = new Array('one', 'two', 'three');
console.log(arr.includes('two', 2));
console.log(arr.includes('one', 1));

It will print false for both statements because for the string 'two', it starts the search from index 2 and for the string 'one', it starts the search from index 1.

It will not find the strings for both cases.

Negative start position:

If you pass a negative value to the second parameter, it will add the array length to it and it will start the search from that index.

let arr : string[] = new Array('one', 'two', 'three');
console.log(arr.includes('two', -3));
console.log(arr.includes('one', -3));

It prints true for both because the search actually starts from index 0. length of the array 3 = 3 - 3 = 0.

includes to search for objects in an array:

You can use includes only if you are passing the same reference of an object.

If you have one object in an array and if you pass the same reference to includes, it will return true. But, if you don't pass the same reference, it will return false.

For example,

let arr : object[] = new Array({name: 'Alex', age: 19}, {name: 'Bob', age: 20});
console.log(arr.includes(arr[0]));
console.log(arr.includes({name: 'Alex', age: 19}));

It will print:

true
false

For the first log, we are passing the first element of the array. includes will find that object in the array because it is the same reference.

For the second log, we are passing one new object. It will not find that object in the array and it will return false.

Subscribe to our Newsletter

Previous
How to add elements to an Array in TypeScript
Next
TypeScript array every method definition and examples