Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
Think of includes() as asking "Is this in the list?"It returns true if the array contains the value,false if not. Super simple! And unlike indexOf(), it can properly detect NaN!
indexOf() !== -1! Returns a boolean, reads like English, and handles NaN correctly unlike indexOf.| Method | Returns | Use When |
|---|---|---|
includes() | true/false | Just checking if value exists |
indexOf() | index or -1 | Need the position/index |
find() | element or undefined | Need the actual element (objects) |
arr.includes(searchElement, fromIndex?)See includes() in action vs other methods
Practical scenarios for includes()
if (!validOptions.includes(input)) return;if (allowedUsers.includes(user)) { /* grant access */ }arr.filter(item => whitelist.includes(item))if (['admin', 'moderator'].includes(role)) { /* ... */ }Returns true/false - perfect for conditionals
Can find NaN unlike indexOf()
Reads like English - very intuitive
Validation, filtering, access control, permissions