As a developer working on a project, I recently faced an issue where I needed to efficiently determine whether a specific value was present in an array. In this tutorial, I will explain how to check if a value exists in an array using TypeScript with practical examples.
Let me show you each method to check if a value exists in a TypeScript array.
Using the includes() Method
One of the simplest ways to check if an array contains a specific value in TypeScript is by using the includes()
method. This method returns true
if the value exists in the array, and false
otherwise. Here’s an example:
const usStates = ['California', 'Texas', 'Florida', 'New York'];
const stateToCheck = 'Texas';
if (usStates.includes(stateToCheck)) {
console.log(`${stateToCheck} exists in the array.`);
} else {
console.log(`${stateToCheck} does not exist in the array.`);
}
In this example, we have an array usStates
containing the names of some US states. We want to check if the value stored in the stateToCheck
variable exists in the array.
Here is the exact output in the screenshot below:

By using the includes()
method, we can easily determine the presence of the value and log an appropriate message to the console.
The includes()
method is an efficient way to check for the existence of a value in an array.
Check out How to Check if an Array is Null or Empty in TypeScript?
Check for Multiple Values
If you need to check for the presence of multiple values in an array, you can use the includes()
method in combination with the logical OR operator (||
). Here’s an example:
const usPresidents = ['George Washington', 'Abraham Lincoln', 'John F. Kennedy', 'Barack Obama'];
const president1 = 'Thomas Jefferson';
const president2 = 'Abraham Lincoln';
if (usPresidents.includes(president1) || usPresidents.includes(president2)) {
console.log('At least one of the presidents exists in the array.');
} else {
console.log('None of the presidents exist in the array.');
}
In this case, we have an array usPresidents
containing the names of some US presidents. We want to check if either president1
or president2
exists in the array.
By using the includes()
method with the OR operator, we can determine if at least one of the values is present in the array.
Check out How to Check if an Array is Not Empty in TypeScript?
Using the indexOf() Method
Another approach to check if a value exists in an array in TypeScript is by using the indexOf()
method. This method returns the index of the first occurrence of the specified value in the array, or -1 if the value is not found. Here’s an example:
const usCities = ['New York City', 'Los Angeles', 'Chicago', 'Houston'];
const cityToCheck = 'Chicago';
if (usCities.indexOf(cityToCheck) !== -1) {
console.log(`${cityToCheck} exists in the array.`);
} else {
console.log(`${cityToCheck} does not exist in the array.`);
}
In this example, we have an array usCities
containing the names of some major US cities. We want to check if the value stored in the cityToCheck
variable exists in the array.
Here is the exact output in the screenshot below:

By using the indexOf()
method and comparing the returned index to -1, we can determine the presence of the value. If the specific element doesn’t exist within the array, indexOf()
returns -1. Hence, this method can be used to check whether an element exists in the array.
Check out How to Check if an Array is Empty in TypeScript?
Case-Insensitive Comparison
By default, the indexOf()
method performs a case-sensitive comparison in TypeScript. If you need to perform a case-insensitive check, you can convert both the array elements and the search value to lowercase (or uppercase) before the comparison. Here’s an example:
const usSports = ['Football', 'Basketball', 'Baseball', 'Ice Hockey'];
const sportToCheck = 'basketball';
if (usSports.map(sport => sport.toLowerCase()).indexOf(sportToCheck.toLowerCase()) !== -1) {
console.log(`${sportToCheck} exists in the array (case-insensitive).`);
} else {
console.log(`${sportToCheck} does not exist in the array (case-insensitive).`);
}
In this example, we have an array usSports
containing the names of popular sports in the US. We want to perform a case-insensitive check to determine if the value stored in the sportToCheck
variable exists in the array.
By converting both the array elements and the search value to lowercase using the toLowerCase()
method, we can perform a case-insensitive comparison.
Using the some() Method
The some()
method is another useful approach to check if a value exists in an array in TypeScript. It tests whether at least one element in the array passes the test implemented by the provided callback function. Here’s an example:
const usInventions = ['Telephone', 'Light Bulb', 'Airplane', 'Internet'];
const inventionToCheck = 'Telephone';
if (usInventions.some(invention => invention === inventionToCheck)) {
console.log(`${inventionToCheck} exists in the array.`);
} else {
console.log(`${inventionToCheck} does not exist in the array.`);
}
In this example, we have an array usInventions
containing the names of some notable US inventions. We want to check if the value stored in the inventionToCheck
variable exists in the array.
By using the some()
method with an arrow function as the callback, we can determine if any element in the array matches the specified value.
The some()
method of Array instances tests whether at least one element in the array passes the test implemented by the provided function.
Check out How to Use Join() Method to Combine Array of Strings in TypeScript?
Custom Comparison Function
The some()
method allows you to provide a custom comparison function as the callback. This enables you to perform more complex comparisons or checks based on specific criteria. Here’s an example:
interface Person {
name: string;
age: number;
country: string;
}
const people: Person[] = [
{ name: 'John Doe', age: 25, country: 'USA' },
{ name: 'Jane Smith', age: 30, country: 'Canada' },
{ name: 'Alice Johnson', age: 35, country: 'USA' },
];
const personToCheck: Person = { name: 'Alice Johnson', age: 35, country: 'USA' };
if (people.some(person => person.name === personToCheck.name && person.age === personToCheck.age)) {
console.log('The person exists in the array.');
} else {
console.log('The person does not exist in the array.');
}
In this example, we have an array people
containing objects of type Person
. Each object represents a person with properties like name
, age
, and country
. We want to check if a specific person object stored in the personToCheck
variable exists in the array.
By using the some()
method with a custom comparison function, we can compare the name
and age
properties of each person object to determine if a match exists.
Checking if an Array Includes an Object
In TypeScript, checking if an array includes an object consists of comparing the object’s properties within the array elements. Here’s an example:
interface Product {
name: string;
price: number;
category: string;
}
const products: Product[] = [
{ name: 'iPhone', price: 999, category: 'Electronics' },
{ name: 'T-Shirt', price: 19.99, category: 'Clothing' },
{ name: 'Sneakers', price: 79.99, category: 'Footwear' },
];
const productToCheck: Product = { name: 'T-Shirt', price: 19.99, category: 'Clothing' };
if (products.some(product =>
product.name === productToCheck.name &&
product.price === productToCheck.price &&
product.category === productToCheck.category
)) {
console.log('The product exists in the array.');
} else {
console.log('The product does not exist in the array.');
}
In this example, we have an array products
containing objects of type Product
. Each object represents a product with properties like name
, price
, and category
. We want to check if a specific product object stored in the productToCheck
variable exists in the array.
By using the some()
method with a comparison function that compares all the relevant properties, we can determine if the object exists in the array.
Conclusion
In this article, I have explained different methods to check if a value exists in an array using TypeScript. We covered the includes()
method for simple value checks, the indexOf()
method for finding the index of a value, and the some()
method for more complex comparisons and checking object properties.
You may also like:
I’m Bijay Kumar Sahoo, and I am honored to be awarded the Microsoft MVP. With over 18 years of experience in the IT industry, I got a chance to work on SharePoint Framework (SPFx) development, TypeScript, React, JavaScript, etc. My journey has taken me through esteemed organizations such as TCS, HP, and KPIT, where I have honed my skills and expanded my expertise. Check out more about me here.