JavaScript tips — Find if an array contains a value using Array.includes

preview_player
Показать описание
The Array includes method is the safest way to find if an array contains a given value in #javascript. You call it on the array you want to search with the value you want to search for. Includes return true if the value exists in the array and false if it does not.

Includes can be used to check if strings, numbers, and objects exist in an array. It does a strict comparision when searching (equivlent to the `===` comparision operator)

Many online resources still recomend using indexOf for this. IndexOf is fine if you need to find the index of a value in an array but is easy to misuse if you are checking if a value exists in an array since it returns number values.

Рекомендации по теме
welcome to shbcf.ru