JavaScript in array
As of ES6 (ES2015), there's a new and easy way to check of an item exists in an array, also known as in array.
This is now possible by using the Array.includes() method which returns a boolean.
Here's a basic example:
const names = ['Alex', 'Sam', 'Blane'];
names.includes('Alex'); //true
names.includes('Red'); //false
Notice how we call .includes
on the array names and how it returns either true or false.
Use cases permalink
This is useful to check if an item exists in an array, however a more important use case is refactoring some if conditions. For example, assuming we want to check a variable status, instead of writing this long if condition:
if (status === 'en_route' || status === 'pending' || status === 'on_hold') {
// do something
}
You can refactor it to a much more readable condition using the javascript in array method you learned above:
const statuses = ['en_route', 'pending', 'on_hold'];
if (statuses.includes(status)) {
// do something
}
Notice how we define the allowed statuses in an array, and then call statuses.includes(status)
, which will return either true or false.
Do you want to Learn JavaScript step by step?
Checkout my interactive course where you read short lessons, solve challenges in the browser and recap topics with Flashcards: