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

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.