Let's sketch the use case. We have some roles that can access a particular page. So only people with that specific role should be able to continue. These valid roles are defined in an array. const roles = ['moderator', 'administrator', 'superman']; ...

Markdown supported · ⌘↵ to post
Nice one Vishwak
.includes() method is definitely my favorite way of checking if array contains a value, it makes code that much more readable.
indexOf() is the "old way" of doing it because includes() is only supported in IE Edge 14+ (no IE11 support).
But when targeting modern browsers, or a transpiler or node.js - includes() is the way to go.
Fun fact - you can also use it on strings to check if string includes a value too:
"Does foobar".includes("foo")
// true
I love these kind of posts 👊! A good follow up would be an Array with Objects 😉
Mahesh
Dad | Web developer
Nice Article buddy. Crisp. I too write almost in the same way. Please have a look at my article on all array methods: