What are the differences between Array.includes() and Array.indexOf() in JavaScript?
Richard W
richard w profile pic

TheArray.includes() andArray.indexOf() methods in JavaScript are used to search for a specific value within an array. While they have similar purposes, there are distinct differences between the two. Here are the differences betweenArray.includes() andArray.indexOf(): 1. Return Value: -Array.includes(): -includes() returns a boolean value (true orfalse) indicating whether the array contains the specified value. -Array.indexOf(): -indexOf() returns the index of the first occurrence of the specified value in the array. If the value is not found, it returns -1. 2. Usage: -Array.includes(): - Useincludes() when you simply need to check if an array contains a particular value. It provides a concise and readable way to perform such a check. -Array.indexOf(): - UseindexOf() when you need to find the index of a specific value in an array. It is useful for retrieving the position of an element or checking if an element exists in the array. 3. Strict Equality Comparison: -Array.includes(): -includes() uses strict equality (===) to compare values. It checks if the value exists in the array without any type coercion. -Array.indexOf(): -indexOf() also uses strict equality (===) to compare values. It returns the index of the first occurrence of the value in the array, or -1 if the value is not found. 4. Multiple Occurrences: -Array.includes(): -includes() does not provide information about the number of occurrences of a value in the array. It only checks if the value exists at least once. -Array.indexOf(): -indexOf() returns the index of the first occurrence of the value in the array. If the value appears multiple times, it only returns the index of the first occurrence. 5. Example: -Array.includes(): - Checking if an array contains a specific value:

1
2
3
4
5
6

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

       const includesValue = array.includes(value);
       

-Array.indexOf(): - Finding the index of a specific value in an array:

1
2
3
4
5
6

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

       const index = array.indexOf(value);
       

In theArray.includes() example,includes() is used to check if the value3 is present in the arrayarray. The resultingincludesValue variable will betrue if it is. In theArray.indexOf() example,indexOf() is used to find the index of the value3 in the arrayarray. The resultingindex variable will be2 since3 is located at index2. Choose the method that suits your specific needs. Useincludes() when you want a boolean result indicating the presence or absence of a value, and useindexOf() when you need the index of the first occurrence of a value in an array or need to check if a value exists in the array.

Similar Questions

What are the differences between JavaScript's Array.includes() and Array.indexOf()?

What are the differences between JavaScript's Array.indexOf() and Array.includes() methods?

What are the differences between Array.filter() and Array.find() in JavaScript?

What are the differences between Array.pop() and Array.shift() in JavaScript?

What are the differences between Array.some() and Array.every() in JavaScript?

What are the differences between JavaScript's String.indexOf() and String.includes()?

What are the differences between Array.forEach() and Array.map() in JavaScript?

What are the differences between JavaScript's Array.find() and Array.findIndex()?

What are the differences between JavaScript's Array.includes() and Array.indexOf() methods when searching for values in arrays?

What are the differences between Array.reduce() and Array.reduceRight() in JavaScript?

What are the differences between JavaScript's Array.join() and Array.toString()?

What are the differences between JavaScript's Array.find() and Array.findIndex() methods?

What are the differences between JavaScript's Array.forEach() and Array.map()?

What are the differences between JavaScript's Array.from() and Array.of() methods?

What are the differences between JavaScript's Array.reverse() and Array.sort()?

What are the differences between JavaScript's Array.filter() and Array.every()?

What are the differences between JavaScript's Array.splice() and Array.slice()?

What are the differences between JavaScript's Array.concat() and Array.join() methods?

What are the differences between JavaScript's Array.push() and Array.pop() methods?

What are the differences between JavaScript's Array.concat() and Array.push() methods?