Nice one Vishwak
Pretty sure I use find the most but the others are good to know too.
.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
Great post!
I think you may have made a typo by accident here:
cost role = 'user';
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: