EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript Array Contains
 

TypeScript Array Contains

Updated April 17, 2023

TypeScript Array Contains

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

TypeScript Array Contains 1

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:

TypeScript Array Contains 2

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:

returns false

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.

  1. TypeScript Functions
  2. TypeScript Operators
  3. TypeScript Versions
  4. What is TypeScript?

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW