Javascript Includes — A Developer's Guide

February 11, 2026
function isPrime(n) {
  if (n <= 1) return false;
  for (let i = 2; i * i <= n; i++) {
    if (n % i === 0) return false;
  }
  return true;
}

// Using the includes method with an array of numbers
const numbers = [1, 2, 3, 4, 5, 6];
console.log(numbers.includes(4)); // Output: true

// Create a new array with only prime numbers
const primeNumbers = numbers.filter(isPrime);
console.log(primeNumbers); // Output: [2, 3, 5]
[2, 3, 5]

Don't you just love when you're trying to check if a value is in an array and you can do it with one simple method? I know I do. The `includes` method has been a game-changer since its introduction in ECMAScript 2015. But - let's be real - it's not just about being able to use `includes` to check for values. It's also about how we can combine it with other methods to create some truly powerful operations. In this case - checking for prime numbers.

What are JavaScript Includes?

So what exactly is `includes`? Simply put - it's a method that returns a boolean value indicating whether an array contains the specified element or not. Now - I know some of you might be thinking "but what about `indexOf`?" And yes - you could use `indexOf` to achieve the same result but let's be real - who wants to deal with `-1` being returned when the value isn't found? Not me.

const numbers = [1, 2, 3, 4, 5];
console.log(numbers.indexOf(4) !== -1); // Output: true
console.log(numbers.includes(4)); // Output: true
true true

When to Use JavaScript Includes

Now that we know what `includes` is and how it works - let's talk about when to use it. Call me old-fashioned but I think it's always best to use the most straightforward approach possible. If you're working with arrays and need to check if a value exists in one of them - `includes` is your best bet.

Practical Examples of JavaScript Includes

We've talked about what `includes` is and when to use it but now let's see some practical examples of how it can be used in your everyday code.

// Example: Checking if a string exists in an array of strings
const colors = ['red', 'green', 'blue'];
console.log(colors.includes('green')); // Output: true
true
// Example: Using includes with filter to create a new array
const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.filter(n => [2,4].includes(n));
console.log(evenNumbers); // Output: [2,4]
[2,4]
// Example: Using includes with reduce to sum up specific values
const numbers = [1, 2, 3, 4, 5];
const sumOfEvenNumbers = numbers.reduce((acc,n) => [2,4].includes(n) ? acc + n : acc ,0);
console.log(sumOfEvenNumbers); // Output: 6
6
// Example: Using includes with every to check if all values exist in another array
const colors1 = ['red', 'green', 'blue'];
const colors2 = ['red', 'green', 'blue', 'yellow'];
console.log(colors1.every(c => colors2.includes(c))); // Output: true
true
javascript includes meme

Conclusion

If you've debugged JavaScript at 2am — like most of us — then you know how valuable having simple yet powerful methods can be. The `includes` method may seem small but its impact on our code should not be underestimated. So next time you find yourself working with arrays — take advantage of this wonderful method!

Ready to take your JavaScript skills to the next level? Visit CodeConverter.co today and discover how our AI-powered code language converter can help simplify your coding process!

Related Articles