Updated April 17, 2023
Introduction to TypeScript Array Contains
TypeScript array contains is a method which determines if an array contains a specific element. This is one of the array methods of TypeScript, which returns true if the array includes the specific element and returns false if not. As TypeScript is similar to JavaScript, array methods will be similar as in JavaScript, but the only syntax wise or notation wise will change. In JavaScript, the array contains is also the same as in TypeScript.
Syntax:
Given below is the syntax of TypeScript array contains/includes:
Array.inlcudes(element, start)
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:
Example #1
Simple TypeScript array contains.
Code:
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'));
Output:
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.
Example #2
Sample array.includes().
Code:
<!DOCTYPE html>
<html>
<body>
<h2>TypeScript array contains</h2>
<script>
var employeeNames = [ 'ABC', 'DEF', 'GHI', 'JKL', 'MNO' ];
employeeBoolean = employeeNames.includes('GHI')
document.write("Boolean value:", employeeBoolean);
</script>
</body>
</html>
Output:
Example #3
Array.includes(element, index).
Code:
<!DOCTYPE html>
<html>
<body>
<h2>TypeScript array contains</h2>
<script>
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/>");
</script>
</body>
</html>
Output:
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.
- As TypeScript is similar to JavaScript, it contains few built-in methods to check whether an array has a particular value or object.
- 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.
Conclusion
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.
Recommended Articles
We hope that this EDUCBA information on “TypeScript Array Contains” was beneficial to you. You can view EDUCBA’s recommended articles for more information.