How to Check if an Array Includes a Value in JavaScript

How to Check if an Array Includes a Value in JavaScript

If you’re working with JavaScript and need to determine whether a specific value exists in an array, There are some methods available to check if an array includes a particular specific value in the array. JavaScript provides several methods to check if an array includes a value.

In this tutorial, you will learn how to check whether array contains or include the specific element or value using the includes() and indexOf method in JavaScript.

To check if an array includes a value in JavaScript, there are several methods you can use. The most common approach is to use the includes() method, which returns a boolean value indicating whether the array contains the specified element. Another approach is to use the indexOf() method, which returns the index of the specified element in the array or -1 if the element is not found.

How to Check if an Array Includes a Value in JavaScript

There are two methods available to check if an array includes or contains a value in javaScript:

  • Method 1: Using the include() method to check an array
  • Method 2: Using the indexOf() method to check an array

Method 1: Using the include() method to check an array

The includes() method is way to check if an array includes a value in JavaScript. This method returns a boolean value, which is true if the value is present in the array, and false otherwise.

The following illustrates the syntax of the includes() method:

array.includes(element,fromIndex);

The includes() accepts two statements:

  • The first argument is the element that can be searched.
  • The fromIndex is the position in the array to which the search starts.

Here’s an example of how to check if an array include a value in javascript using include():

const arr = [1, 2, 3, 4, 5];
const value = 3;

if (arr.includes(value)) {
  console.log('Value found in the array');
} else {
  console.log('Value not found in the array');
}

In this example, The includes() method to check whether the value 3 exists in the array. If the value is found in the array, the code inside the if statement will be executed, otherwise, the code inside the else statement will be executed.

Method 2: Using the indexOf() method to check an array

The indexOf() method is used to find the index of a particular element in an array. If the element is not present in the array, it returns -1. Therefore, you can use this method to check whether a particular value exists in the array or not.

The following represents the syntax of the indexOf() method:

Array.indexOf(search, startIndex)

The indexOf() takes 2 parameters:

  • The search argument is the element that you want to find in the array.
  • The startIndex is an array index at which the function starts the search.

Here’s an example of how to check if an array contains a value in javascript using indexOf():

const arr = [1, 2, 3, 4, 5];
const value = 3;

if (arr.indexOf(value) !== -1) {
  console.log('Value found in the array');
} else {
  console.log('Value not found in the array');
}

In this example, The indexOf() method to check whether the value 3 exists in the array. If the value is found in the array, the code inside the if statement will be executed, otherwise, the code inside the else statement will be executed.

Difference between Array.includes() and Array.indexOf()

The javascript method Array.includes() was introduced in ECMAScript 6. The includes() method is used to find out whether the element is included in the array or not. It returns the boolean value: true or false.

We use array .indexOf() to find out whether the element is present in the array or not. But it doesn’t return a boolean. It returns the first index of the element found in the array, or it will return -1 (which represents that the element is not found).

Unlike array indexOf(), which uses Strict Equality Comparison, it includes compares using the SameValueZero equality algorithm. What that means that you can detect if the array includes a NaN.

Conclusion

In conclusion, checking if an array includes a value in JavaScript is a simple task that can be accomplished using the includes() or indexOf() methods. By understanding the differences and optional arguments of these methods, you can choose the approach that best fits your specific use case.

Recommended Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *