How to Easily Check Values in JavaScript Arrays Using includes()

Checking if an array contains a specific value in JavaScript is a must-know concept for beginners and for coding interviews. Here I’ll explore two practical methods with clear examples and simple explanations: .includes() and .indexOf(). If arrays and loops are new to you, see loops, arrays. Suppose we have an array like apple, apple, banana, and mango. If we want to know if this array contains banana or not, JavaScript provides handy methods for that. Think of it like checking a grocery list to see if milk is on it before going to the shop. The same idea applies here: check if a value exists before using it. If you’re reading values from form inputs, you might want to check them first too. For that, see input values.

JavaScript Array Includes

  How to Easily Check Values in JavaScript Arrays Using includes()  1   The .includes() method answers the question directly with a boolean. It returns true when the element is present and false when it is not. Create the array.
const fruits = ['apple', 'apple', 'banana', 'mango'];
  How to Easily Check Values in JavaScript Arrays Using includes()  2   Check if banana exists.   How to Easily Check Values in JavaScript Arrays Using includes()  3  
console.log(fruits.includes('banana')); // true
  How to Easily Check Values in JavaScript Arrays Using includes()  4   Check a value that is not present.   How to Easily Check Values in JavaScript Arrays Using includes()  5  
console.log(fruits.includes('grapes')); // false
  How to Easily Check Values in JavaScript Arrays Using includes()  6   You can store the result in a variable if you want to reuse it.   How to Easily Check Values in JavaScript Arrays Using includes()  7  
const hasBanana = fruits.includes('banana');
console.log(hasBanana); // true
  How to Easily Check Values in JavaScript Arrays Using includes()  8  

IndexOf method

The .indexOf() method returns the index of the first match or -1 if the element is not found. You can convert that result into a boolean check.   How to Easily Check Values in JavaScript Arrays Using includes()  9   Check mango using indexOf with a comparison.   How to Easily Check Values in JavaScript Arrays Using includes()  10  
const fruits = ['apple', 'apple', 'banana', 'mango'];

console.log(fruits.indexOf('mango') !== -1); // true
  How to Easily Check Values in JavaScript Arrays Using includes()  11   Check a value that does not exist.   How to Easily Check Values in JavaScript Arrays Using includes()  12  
console.log(fruits.indexOf('grapes') !== -1); // false
  How to Easily Check Values in JavaScript Arrays Using includes()  13   If an element is not present, .indexOf() returns -1. Comparing against -1 gives you a clean true or false. This approach works well, though .includes() is the simpler and more readable method for this specific task. If your array can contain duplicates and you need to count how many times a value appears, see count frequencies.

Final thoughts

To check if an array contains a value, .includes() is the most direct and readable method. .indexOf() is another option by comparing the returned index against -1. Both are important to learn and apply in real projects where you often need to confirm a value exists before using it.

Leave a Comment