Categories
JavaScript Answers

How to check variable equality against a list of values with JavaScript?

Spread the love

Sometimes, we want to check variable equality against a list of values with JavaScript.

In this article, we’ll look at how to check variable equality against a list of values with JavaScript.

How to check variable equality against a list of values with JavaScript?

To check variable equality against a list of values with JavaScript, we can use the array includes method.

For instance, we write

if ([1, 47, 28].includes(foo)) {
  // ...
}

to call [1, 47, 28].includes with foo to check if foo is one of 1, 47 or 28.

Conclusion

To check variable equality against a list of values with JavaScript, we can use the array includes method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *