EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript Array Contains
Secondary Sidebar
TypeScript Tutorial
  • TypeScript Array
    • TypeScript Array of Objects
    • Methods TypeScript Array
    • TypeScript remove item from array
    • TypeScript add to array
    • TypeScript Array Contains
  • TypeScript Basic and Advanced
    • What is TypeScript?
    • Typescript Examples
    • TypeScript Versions
    • TypeScript Operators
    • JavaScript dump object
    • JavaScript get Method
    • Webpack ReactJS
    • Code Generator JavaScript
    • JavaScript Projects
    • Call Stack JavaScript
    • JavaScript Projects GitHub
    • JavaScript Filter Function
    • JavaScript nan
    • JavaScripttimestamp
    • TypeScript loop
    • CoffeeScript
    • TypeScript Webpack
    • setTimeout TypeScript
    • DHTMLX
    • CoffeeScript for loop
    • TypeScript number
    • JavaScript export module
    • TypeScript string contains
    • TypeScript Inheritance
    • TypeScript get
    • TypeScript undefined
    • TypeScript Global Variable
    • TypeScript Dictionary
    • TypeScript Generic
    • TypeScript Cast Object
    • TypeScript Optional Parameters
    • TypeScript? switch
    • TypeScript promise
    • TypeScript tuple
    • TypeScript Hashmap
    • TypeScript let
    • TypeScript Getter
    • TypeScript Pattern Matching
    • TypeScript number to string
    • TypeScript substring
    • TypeScript?lambda
    • TypeScript UUID
    • TypeScript JSDoc
    • TypeScript Decorators
    • Typescript for loop
    • TypeScript HTTP Request
    • TypeScript Abstract Class
    • TypeScript Question Mark
    • TypeScript Nullable
    • TypeScript reduce
    • TypeScript Mixins
    • TypeScript keyof
    • TypeScript string to number
    • TypeScript JSON parse
    • TypeScript const
    • TypeScript declare module
    • TypeScript String
    • TypeScript filter
    • TypeScript Multiple Constructors
    • TypeScript? Set
    • TypeScript string interpolation
    • TypeScript instanceof
    • TypeScript JSON
    • TypeScript Arrow Function
    • TypeScript generator
    • TypeScript namespace
    • TypeScript default parameter
    • TypeScript cast
    • TypeScript babel
    • Typescript Key-Value Pair
    • TypeScript if
    • TypeScript keyof Enum
    • TypeScript wait
    • TypeScript Optional Chaining
    • TypeScript JSX
    • TypeScript Version Check
    • TypeScript Unit Testing
    • TypeScript Handbook
    • TypeScript module
    • TypeScript Extend Interface
    • TypeScript npm
    • TypeScript pick
    • TypeScript Interface Default Value
    • JavaScript import module
    • Obfuscate Javascript
    • TypeScript basics
    • setInterval TypeScript
  • Type of Union
    • TypeScript Object Type
    • TypeScript type check
    • TypeScript promise type
    • TypeScript JSON type
    • TypeScript Union Types
    • TypeScript typeof
    • TypeScript Types
  • Function Of Array
    • TypeScript Function Interface
    • TypeScript Functions
    • TypeScript Export Function
    • TypeScript function return type

TypeScript Array Contains

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.

Syntax:

Given below is the syntax of TypeScript array contains/includes:

Array.inlcudes(element, start)

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

So this is the method used to check for specific values in an array. This method will receive 2 parameters.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,818 ratings)
  • 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

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 –

  1. TypeScript Functions
  2. TypeScript Operators
  3. TypeScript Versions
  4. What is TypeScript?
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more