Introduction to TypeScript Array Contains
Given below is the syntax of TypeScript array contains/includes:
So this is the method used to check for specific values in an array. This method will receive 2 parameters.
- element: Mandatory parameter, which is the element to search for.
- start: It is an optional parameter, and the default value being 0, i.e., position in array where the search should start.
Returns a Boolean value ‘true’ or ‘false’.
Examples of TypeScript Array Contains
Given below are the examples of how array contains/includes works in TypeScript:
Simple TypeScript array contains.
const array1 = [11, 22, 33, 44, 55];
console.log("Boolean value:", array1.includes(44));
const fruits = ['mango', 'apple', 'orange'];
console.log("Boolean value:", fruits.includes('go'));
Here, in this example, array1 has 5 elements where the user searches for 44 element, which returns true.
And fruits array has 3 elements out of which the user searches for ‘go’, which is not present in the fruits array, hence returns false.
<h2>TypeScript array contains</h2>
var employeeNames = [ 'ABC', 'DEF', 'GHI', 'JKL', 'MNO' ];
employeeBoolean = employeeNames.includes('GHI')
document.write("Boolean value:", employeeBoolean);
<h2>TypeScript array contains</h2>
employeeBoolean1 = ['ABC', 'DEF', 'GHI', 'JKL', 'MNO'].includes('JKL', 2)
employeeBoolean2 = ['ABC', 'DEF', 'GHI', 'JKL', 'MNO'].includes('JKL', 3)
employeeBoolean3 = ['ABC', 'DEF', 'GHI', 'JKL', 'MNO'].includes('JKL', 4)
document.write("Boolean value1:", employeeBoolean1 + "<br/>");
document.write("Boolean value2:", employeeBoolean2 + "<br/>");
document.write("Boolean value3:", employeeBoolean3 + "<br/>");
Here, in this example, employeeBoolean1, in which we search for JKL to search from the 2nd index. So on searching from the 2nd index, JKL is found at 3rd position. employeeBoolean2, in which we search for JKL from 3rd position, so array includes returns true. employeeBoolean3, in which we search for JKL from 4th position, but JKL is in 3rd position, so it returns false.
Rules and Regulations for Array Contains/Includes
- As arrays are one of the most used data structures in the IT field, while dealing with a list of array items, users must look for a specific value in the array list.
- And the simplest way to check primitive values in the array to use the includes() method.
- If we look at the above examples, the simplest way to check for a primitive value in the array is to use the include() method.
- In some cases, where the user needs the exact location of a specified element, indexOf(element) can also be used to first get the element’s location.
- If the receiver object of the method indexOf call has includes a method, with both having the same parameters, it has a suggestion like types, String, Array, ReadonlyArray, and other typed arrays.
- EcmaScript 2015 has added includes() for String and EcmaScrpt 2016 has added includes() for arrays.
- Includes() method is generic and hence does not require this value to be Array object, but can be applied to Array-like objects.
- All modern browsers support includes() method. But if you are using Internet Explorer or any old browser, we need to use indexOf.
- Technically, includes() method uses the sameValueZero algorithms to know whether a specific element in an array is found or not.
- fromIndex, which we are using as the second parameter, is an optional parameter. If it searches for the first element at fromIndex, then it is a positive value.
- Or at array length plus fromIndex is for negative values, and the default is 0.
- While comparing strings and characters, the array contains/ includes() method is case sensitive.
- If fromIndex is greater or equal to array length, the Boolean value false is returned; hence array will not be searched.
- If fromIndex is negative, a computed index is calculated, which is to be used as a position at where the search is to begin. And if this value is less than or equal to -1 * length of the array, the complete array will be searched.
With this, we conclude the topic ‘TypeScript Array contains’, also know as array includes. We have seen what array contains mean and how is the syntax built to find the specified values in the array. We have also seen how the array includes() method is used with few examples. Each example is explained in a way such that people can easily understand what parameters are to be given and when and how it affects the output. We have also seen some of the rules and regulations for the TypeScript array contains method, which will give a clear idea on how and in what way this array includes() method can be used in real-world coding.
This is a guide to TypeScript Array Contains. Here we discuss the introduction, examples, rules and regulations for array contains/includes. You may also have a look at the following articles to learn more –