The Wayback Machine - https://web.archive.org/web/20240912174241/https://www.geeksforgeeks.org/javascript-array-includes-method/
Open In App

JavaScript Array includes() Method

Last Updated : 12 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The includes() method returns true if an array contains a specified value. Conversely, it returns false if the value is not found. This method simplifies checking for the presence of an element within an array, providing a straightforward boolean result.

Syntax

array.includes(searchElement, start);

Parameters

  • searchElement: This parameter holds the element that will be searched.
  • start: This parameter is optional and it holds the starting point of the array, where to begin the search the default value is 0.

Return Value

  • It returns a Boolean value i.e., either True or False.

Example 1: Searching for a number in an array

Here, the method will search for element 2 in that array.

javascript
// Taking input as an array A
// having some elements.
let A = [1, 2, 3, 4, 5];

// includes() method is called to
// test whether the searching element
// is present in given array or not.
a = A.includes(2)

// Printing result of includes().
console.log(a);

Output
true

Example 2: Searching for a string in an array

Here, the method will search for the element ‘cat’ in that array and returns false as ‘cat’ is not present in the array.

javascript
// Taking input as an array A
// having some elements.
let name = ['gfg', 'cse', 'geeks', 'portal'];

// includes() method is called to
// test whether the searching element
// is present in given array or not.
a = name.includes('cat')

// Printing result of includes()
console.log(a);

Output
false

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete referencearticle.

Supported Browsers:

JavaScript Array includes() Method- FAQs

What is the array includes method in JavaScript?

The includes() method in JavaScript determines whether an array contains a certain element, returning true if it does and false if it does not

What happens if the value is not found in the array?

If the value is not found in the array, the includes() method returns false. It performs a strict equality check for the specified element and, if no match is found, the result is false.

Does the includes() method modify the original array?

No, the includes() method does not modify the original array. It only checks for the presence of the specified element and returns a boolean result.

Can the includes() method be used with multi-dimensional arrays?

The includes() method can be used with arrays containing any type of elements, including objects, strings, numbers, undefined, and null. However, for objects, the method checks for object reference equality, not content equality.

What are some common use cases for the includes() method?

Common use cases for the includes() method include:

  • Checking if an array contains a specific value.
  • Validating user input against a list of allowed values.
  • Ensuring that an array does not contain duplicate entries before adding a new element.

Previous Article
Next Article

Similar Reads

String includes() Method in JavaScript
JavaScript str.includes() function is used to check if the argument string is present in the given string or not. This function is case-sensitive. The syntax of this function is as follows: str.includes(searchString, position) Arguments The first argument to this function searchString is the string to be searched in the given string. The second arg
2 min read
JavaScript typedArray.includes() Method
The typedArray.includes() is an inbuilt function in JavaScript which is used to check whether a particular element is included by the given typedArray or not and accordingly it returns true and false. Syntax: typedarray.includes(Element, Index); Parameters: It accepts two parameter which are specified below- Element: It is the element which are bei
2 min read
What is includes() Method in JavaScript ?
In JavaScript, the includes() method is used to determine whether a string contains another string or a specified value. It returns true if the string contains the specified value, and false otherwise. Syntax:string.includes(searchValue, startIndex)string: The string in which to search for the specified value.searchValue: The value to search for wi
1 min read
JavaScript TypedArray.prototype.includes() Method
In JavaScript the TypedArray.prototype.includes() method is used to check that the given typed array contains a specific given element or not, if it contains the element it returns true otherwise false. SyntaxtypedArray.includes(searchElement)ORtypedArray.includes(searchElement, fromIndex)ParameterssearchElement: The value to search for. fromIndex:
1 min read
JavaScript String includes() Method
The includes() method returns true if a string contains a specified string. Otherwise, it returns false. Note: The includes() method is case sensitive i.e. it will treat the Uppercase Characters and Lowercase Characters differently. Syntax:string.includes(searchvalue, start)Parameters:search value: It is the string in which the search will take pla
3 min read
How to check whether an array includes a particular value or not in JavaScript ?
In this article, we will discuss the construction of an array followed by checking whether any particular value which is required by the user is included (or present) in the array or not. But let us first see how we could create an array in JavaScript using the following syntax- Syntax: The following syntax would be helpful for any user for the cre
3 min read
How do I check if an Array includes a value in JavaScript?
We are going to learn how to check if a value is present in an array or not. We will need the array we want to search through and the target element we are looking for. JavaScript arrays serve as convenient containers for holding lists of elements, accessible through a single variable. Once we have a target element we can perform various search alg
4 min read
How to check if an array includes an object in JavaScript ?
Check if an array includes an object in JavaScript, which refers to determining whether a specific object is present within an array. Since objects are compared by reference, various methods are used to identify if an object with matching properties exists in the array. These are the following approaches by using these we can check if an array incl
3 min read
TypeScript Array includes() Method
The TypeScript Array.includes() method checks if an array contains a specified element, returning true if found and false otherwise. It performs a strict equality comparison (===), works with all data types, and correctly identifies NaN in arrays. Syntaxarray.includes(searchElement[, fromIndex])Parameters:searchElement: The value to search for with
2 min read
Ember.js Ember.NativeArray includes() Method
Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of webs
4 min read
Ember.js MutableArray includes() Method
Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of webs
4 min read
Ember.js EmberArray includes() Method
Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of webs
4 min read
Ember.js ArrayProxy includes() Method
Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of webs
3 min read
TypeScript String includes() Method
The includes() method in TypeScript, inherited from JavaScript, is used to determine whether a given string contains a specified substring. This method returns a boolean value: true if the substring is found and false otherwise. SyntaxLet's start by understanding the syntax of the includes() method. string.includes(searchValue, fromIndex?: number):
2 min read
Node.js Buffer.includes() Method
Buffer is a temporary memory storage which stores the data when it is being moved from one place to another. It is like an array of integers. The Buffer.includes() method checks whether the provided value is present or included in the buffer or not. Syntax: buffer.includes( value, byteOffset, encoding );Parameters: This method accept three paramete
2 min read
Lodash _.includes() Method
Lodash _.includes() method is used to find whether the value is in the collection or not. If the collection is a string, it will be tested for a value sub-string, otherwise SameValueZero() method is used for equality comparisons. If the index is given and is negative, the value is tested from the end indexes of the collection as the offset. Syntax:
3 min read
How to Check if an Array Includes an Object in TypeScript ?
In TypeScript, checking if an array includes an object consists of comparing the object's properties within the array elements. We can compare and check this using three different approaches some method, find method, and includes method. There are several ways to check if an array includes an object in TypeScript which are as follows: Table of Cont
3 min read
What does Bootstrap Package includes ?
It contains HTML and CSS-based design templates for typography, forms, buttons, and other interface components, as well as optional JavaScript extensions. A website has many different elements such as headings, lists, tables, images, buttons, forms, etc. All these fundamental HTML elements have been styled and enhanced with extensible classes. The
3 min read
What is includes in Pug View Engine ?
In Pug, "includes" are a way to modularize and reuse code by importing separate Pug files into a main Pug file. This helps in organizing code more efficiently and reduces duplication of code by allowing you to include common sections or components across multiple pages easily. In simple words, Includes allows you to insert the contents of one Pug f
3 min read
AngularJS Includes
AngularJS includes, also called as ng-include directive, allows you to insert external HTML content dynamically into an AngularJS application. This capability is particularly useful for modularizing applications, improving code organization, and enhancing code reusability. Syntax: <element ng-include=" "> content... <element>Key Feature
3 min read
How to extend an existing array with another array without creating a new array in JavaScript ?
JavaScript arrays are a powerful data structure that allows you to store and manipulate collections of data. Often, we may need to combine or extend two or more arrays into a single array. It's easy to create a new array by concatenating two or more arrays but if we want to extend an existing array without creating a new array. There are two easy w
2 min read
How to compare two JavaScript array objects using jQuery/JavaScript ?
In this article, we are given two JavaScript array/array objects and the task is to compare the equality of both array objects. These are the methods to compare two JavaScript array objects: Using jQuery not() methodUse the sort() functionUse JSON.stringify() functionUsing every() and indexOf()Using Lodash _.isEqual() MethodApproach 1: Using jQuery
3 min read
How does Promise.all() method differs from Promise.allSettled() method in JavaScript ?
In this article, we will first understand in brief the Promise.all() as well as Promise.allSettled() methods and then we will try to visualize how they differ from each other through some theoretical as well as some coding examples. Both Promise.all() and Promise.allSettled() methods are the methods of a Promise object (which is further a JavaScrip
4 min read
How does Promise.any() method differs from Promise.race() method in JavaScript ?
In this article, we will first try to understand how we may declare or use Promise.any() and Promise.race() methods followed by some facts which will eventually help us to understand how they differ from each other (through theoretical as well as coding examples). Let us first quickly understand in brief about both the methods followed by their syn
5 min read
How toReversed() method is different from reverse() method in JavaScript
In JavaScript, reverse() and toReversed() methods do the same job as reversing but toReversed() does not change the original array. The reverse() method changes the original array. Below we have explained both methods. Table of Content Reverse() MethodtoReversed() methodReverse() MethodThe reverse() method is a built-in method for arrays in JavaScr
2 min read
Implement polyfill for Array.prototype.map() method in JavaScript
In this article, we will learn how to implement a polyfill for an Array.prototype.map() method in JavaScript. What is a polyfill? A polyfill is a piece of computer code written to implement a feature in a browser that does not yet support it. It could be because of the older version of the browser you are using, or because the new version of the br
3 min read
JavaScript Array map() Method
JavaScript Array map() Method method creates a new array with the results of a called function for every array element. This function calls the argument function once for each element of the given array in order. Syntax: // Arrow function map((element) => { /* … */ }) map((element, index) => { /* … */ }) map((element, index, array) => { /*
3 min read
JavaScript Array valueOf() Method
The JavaScript Array valueOf() method in JavaScript is used to return the array. It is a default method of the Array Object. This method returns all the items in the same array. It will not change the original content of the array. It does not contain any parameter values. Syntax: array.valueOf()Parameters: This method does not accept any parameter
2 min read
How to use Array.prototype.reduce() method in JavaScript ?
The Array.prototype.reduce() method is used in an array to return a single value from an array after executing the user-supplied callback function on each element of the array. It transverses from the left-most-element to the right-most-element of the given array. Array.prototype.reduce() can be called using two ways. Callback function: The functio
3 min read
Which one method use to delete an element from JavaScript array (slice or delete) ?
We can use both methods to delete an element from a javascript array. The answer is solely dependent on the application and on the programmer. What slice() method does? The slice method, as the name suggests, slices an array and returns a copy of the new array. This method does not affect the main array, it just returns the copy of the array. We ca
4 min read