javascript / beginner
Snippet
Checking for Elements with .includes()
The .includes() method is a simple way to check if an array contains a specific value. It returns a boolean (true or false) and is much cleaner than using older index-based searches.
snippet.js
1
2
3
4
const pets = ['dog', 'cat', 'bird'];const hasCat = pets.includes('cat');console.log(hasCat); // true
nodejs
Breakdown
1
const pets = ['dog', 'cat', 'bird'];
Creates an array of strings representing pet types.
2
pets.includes('cat')
Scans the array for the string 'cat' and returns true if found.