Tip 6: Check Existence in an Array with Includes()
It’s easy to get so caught up in the big exciting changes in a language (such as the spread operator, which you’ll see in a moment) that you miss the small changes that simplify common tasks.
Arrays now have an easy improvement to handle a common problem: testing existence. Testing existence is an important action, and it’s crucial in everything from ternaries (Tip 18, Check Data Quickly with the Ternary Operator), to short circuiting (Tip 19, Maximize Efficiency with Short Circuiting), to most conditionals in general.
example, if you want to see if an array contains a certain string, you check
to see if the string has a position (position being another feature of an iterable).
If the position exists, you’ll get the index. If not, you get
-1. The problem is
that the index can be 0, which evaluates to false (also known as being falsy).
This means the existence can be true, but the check can evaluate to false.