---
title: "Discussion on \"JavaScript check if array contains a value\" | Hashnode"
description: "Discussion on \"JavaScript check if array contains a value\". Let's sketch the use case. We have some roles that can access a particular page.\nSo only people with that specific role should be able to continue.\nThese valid roles are defined in an array.\nconst roles = ['moderator', 'administrator', 'superman'];\n\n..."
url: "https://hashnode.com/posts/javascript-check-if-array-contains-a-value/61f262f3805bdf3ebd486fbd"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [JavaScript check if array contains a value](https://h.daily-dev-tips.com/javascript-check-if-array-contains-a-value?utm_source=hashnode&utm_medium=feed)

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'\]; ...

Jan 27, 2022 · 2 min read · h.daily-dev-tips.com  
Read the article

-   [#javascript](https://hashnode.com/tag/javascript)
-   [#es6](https://hashnode.com/tag/es6)

## Written by

[Chris Bongers](https://hashnode.com/@dailydevtips)

@dailydevtips · Cape Town, SA

  
[View profile](https://hashnode.com/@dailydevtips)

Looking to get into development? As a full-stack developer I guide you on this journey and give you bite sized tips every single day 👊

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.

## Responses 5

Markdown supported · ⌘↵ to post

[Mahesh](https://hashnode.com/@urstrulyvishwak)

[Feb 10, 2022](https://hashnode.com/posts/javascript-check-if-array-contains-a-value/61f262f3805bdf3ebd486fbd/comment/620522d79e350308247fd749)

Nice Article buddy. Crisp. I too write almost in the same way. Please have a look at my article on all array methods:

[vkglobal.hashnode.dev/some-x-array-utility-functi…](https://vkglobal.hashnode.dev/some-x-array-utility-functions-in-js)

[Chris Bongers](https://hashnode.com/@dailydevtips)

[Feb 11, 2022](https://hashnode.com/posts/javascript-check-if-array-contains-a-value/61f262f3805bdf3ebd486fbd/comment/620522d79e350308247fd749#reply-6205fae09e350308247fe9d4)

Nice one Vishwak

[Andrew Baisden](https://hashnode.com/@andrewbaisden)

[Jan 29, 2022](https://hashnode.com/posts/javascript-check-if-array-contains-a-value/61f262f3805bdf3ebd486fbd/comment/61f548db6bb59d5767db6580)

Pretty sure I use `find` the most but the others are good to know too.

[Chris Bongers](https://hashnode.com/@dailydevtips)

[Jan 30, 2022](https://hashnode.com/posts/javascript-check-if-array-contains-a-value/61f262f3805bdf3ebd486fbd/comment/61f548db6bb59d5767db6580#reply-61f66b846bb59d5767db7ec3)

Find is pretty amazing 🤯

[Nauris](https://hashnode.com/@pyronaur)

[Jan 27, 2022](https://hashnode.com/posts/javascript-check-if-array-contains-a-value/61f262f3805bdf3ebd486fbd/comment/61f2e2ed22a38747671d6cd6)

`.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
```

[Chris Bongers](https://hashnode.com/@dailydevtips)

[Jan 28, 2022](https://hashnode.com/posts/javascript-check-if-array-contains-a-value/61f262f3805bdf3ebd486fbd/comment/61f2e2ed22a38747671d6cd6#reply-61f390cd22a38747671d79f6)

Yep very true indeed :)

[Bence Hornyák](https://hashnode.com/@benceHornyak)

[Jan 27, 2022](https://hashnode.com/posts/javascript-check-if-array-contains-a-value/61f262f3805bdf3ebd486fbd/comment/61f2d6c322a38747671d6b7a)

Great post!

I think you may have made a typo by accident here:

```
cost role = 'user';
```

[Chris Bongers](https://hashnode.com/@dailydevtips)

[Jan 28, 2022](https://hashnode.com/posts/javascript-check-if-array-contains-a-value/61f262f3805bdf3ebd486fbd/comment/61f2d6c322a38747671d6b7a#reply-61f390b452c78247427494d3)

Yep, thanks for noting that, fixed it

[Dev By RayRay](https://hashnode.com/@devbyrayray)

[Jan 27, 2022](https://hashnode.com/posts/javascript-check-if-array-contains-a-value/61f262f3805bdf3ebd486fbd/comment/61f272708a9bc14421f57ad6)

I love these kind of posts 👊! A good follow up would be an Array with Objects 😉

[Chris Bongers](https://hashnode.com/@dailydevtips)

[Jan 27, 2022](https://hashnode.com/posts/javascript-check-if-array-contains-a-value/61f262f3805bdf3ebd486fbd/comment/61f272708a9bc14421f57ad6#reply-61f281ae8a9bc14421f57e14)

Nice one,

Will definitely re-update one with objects.
