How do you find if an element exists in an array in javascript?

Examples

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.includes("Mango");

Try it Yourself »

Start the search at position 3:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.includes("Banana", 3);

Try it Yourself »

Definition and Usage

The includes() method returns true if an array contains a specified value.

The includes() method returns false if the value is not found.

The includes() method is case sensitive.

Syntax

array.includes(element, start)

Parameters

Parameter Description
element Required.
The value to search for.
start Optional.
Start position. Default is 0.

Return Value

Type Description
A boolean true if the value is found, otherwise false.

Browser Support

includes() is an ECMAScript7 (ES7) feature.

ES7 (JavaScript 2016) is supported in all modern browsers:

Chrome Edge Firefox Safari Opera
Yes Yes Yes Yes Yes

includes() is not supported in internet Explorer or Edge 13 (or earlier).



In a programming language like Javascript, to check if the value exists in an array, there are certain methods. To be precise, there are plenty of ways to check if the value we are looking for resides amongst the elements in an array given by the user or is predefined. Let's discuss these methods one by one using various examples.

indexof() method

The indexof() method in Javascript is one of the most convenient ways to find out whether a value exists in an array or not. The indexof() method works on the phenomenon of index numbers. This method returns the index of the array if found and returns -1 otherwise. Let's consider the below code:

Output

The above code prints the given out because the value is already present in the array. It is quite easy to understand that the expected value is present at position 0. Thus, the indexof() method tells you that the value expected is present in the given array.

includes() method

The includes() method is one such method using which we can easily find out whether the expected value exists in the given array. There are various ways to use include() method. This method returns a Boolean value i.e. true if the value exists and false if it incorrect. The includes() method can be used in various ways to find out if the value exists. To name a few, take a look at the below examples to understand.

In the above method, we have defined two variables as shown. The includes() methods return true because the value which we are looking for is already present in the given array. If the value was not present in the array, the includes() methods might have returned false.

Another way of using the includes() method is by assigning the index value through which the element we are looking for is generated as output. See the below code for reference.

In the above code snippet, we have defined the variable "actors" which the value. We have also defined a variable "names" which would return true or false, if the includes() method returns the shown result. The code above will return true since the value and the index number have been correctly assigned and would return the output.

The above examples are some of the predefined methods that we have used to check whether an element exists in the array or not. We have another approach to find out an array element using loops. Let's discuss how can we check if the element exists in an array using loops as shown in the below code snippet.

Using loops

Output

status: Present status: Absent

In the above code snippet, we have defined an array with some values in the form of strings. We have also defined a function under which the variable status is used as a string to mark if the elements are present in the program. The logical flow of the program is to traverse through each element present in the array and check if the element is present. If the value exists in the array it will display "Present" and "Absent" accordingly.

Summary

Javascript is dynamic and flexible programming as well as a scripting language. It is a powerful developer-friendly natured language that lets you do single stuff in multiple ways so that our learning curve remains steep. In this article, we discussed how we can easily carve different ways through which we can easily find whether the given array consists of the desired value or not. We also came across certain methods and generic programming examples that are not just easy to understand but can be implemented with no absolute knowledge. We have used indexof() and includes() methods in this article since they are the most used methods whenever it is required to find out the value enshrined in an array. We also came across loops through which one can easily find out by the normal linear search traversal as we do in the generic programming paradigms.

How do you know if an element is present in an array?

JavaScript Array includes() The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found. The includes() method is case sensitive.

How do you find something in an array?

Use filter if you want to find all items in an array that meet a specific condition. Use find if you want to check if that at least one item meets a specific condition. Use includes if you want to check if an array contains a particular value. Use indexOf if you want to find the index of a particular item in an array.

How do you check if an array does not contain a value JavaScript?

To check if an array doesn't include a value, use the logical NOT (!) operator to negate the call to the includes() method. The NOT (!) operator returns false when called on a true value and vice versa.

How do you check if an item exists in an array TypeScript?

To check if a TypeScript array contains an object:.
Pass a function to the Array. find() method..
Check whether the identifier of the object is equal to a specific value and return true if it is..
Array. find will return the object if the conditional check is satisfied at least once..

Chủ đề