Array.includes() method looks through an entire array and determines if the specified value exists in the array, returning a boolean value accordingly.
valueToFindis the value that is being searched for
fromIndexis the index at which to start looking for the
📝 Note: If
valueToFind is not specified, it defaults to
Real World Examples 🌎
Let's imagine that a manager wanted to confirm whether one of their employees,
Jose, was on the
designTeam or the
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 ✅
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
firstname.lastname@example.org. If you're enjoying my posts, please subscribe below! 👇