Array.includes()

The Array.includes() method looks through an entire array and determines if the specified value exists in the array, returning a boolean value accordingly.

Syntax 📝

Array.includes(*valueToFind*[, fromIndex])

Parameters

  • valueToFind is the value that is being searched for
  • fromIndex is the index at which to start looking for the valueToFind

📝 Note: If valueToFind is not specified, it defaults to 0.

Real World Examples 🌎

Let's imagine that a manager wanted to confirm whether one of their employees, Jose, was on the designTeam or the devTeam.

They could use the Array.includes() method to confirm one way or the other rather quickly.

In the example below we have an array of the two teams containing their respective employees:

const devTeam = ['Blerim', 'Dmitrii', 'Jose', 'Tim', 'Vrishali', 'Zach'];
const designTeam = ['April', 'Brady', 'Joslyn', 'Mel', 'MK', 'Nikita']

// check if devTeam contains 'Jose'
devTeam.includes('Jose') // returns true

// check if devTeam contains 'Mitch'
devTeam.includes('Mitch') // returns false

// check if designTeam contains 'April' on second half of team
designTeam.includes('April', (designTeam.length / 2)) // returns false

Wrapping Up ✅

The Array.includes() method is a really handy way to quickly check if a given array does - or does not - include a particular value.

Stay Tuned 📺

If you have any questions or improvements, or if you'd like to see additional examples, feel free to reach out to me anytime at tim@timwheeler.com. If you're enjoying my posts, please subscribe below! 👇