Categories
JavaScript Answers

How to use string includes() in a JavaScript switch case?

Spread the love

Sometimes, we want to use string includes() in a JavaScript switch case.

In this article, we’ll look at how to use string includes() in a JavaScript switch case.

How to use string includes() in a JavaScript switch case?

To use string includes() in a JavaScript switch case, we should replace switch and case with if statements.

For instance, we write:

const id = "someId";
if (id.includes('product')) {
  //...
} else if (id.includes('user')) {
  //...
}

to call id.includes with the substrings we’re checking and run the code we want according to the checks.

Conclusion

To use string includes() in a JavaScript switch case, we should replace switch and case with if statements.

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 *