Search⌘ K

Tip 6: Check Existence in an Array with Includes()

Understand how to use the includes() method to check for the existence of values in arrays, avoiding common pitfalls with index checks. Explore practical examples that simplify conditionals and improve code clarity when managing data collections.

We'll cover the following...

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.

Testing existence

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 ( ...