How to use expression inside switch case statement with JavaScript?

Sometimes, we want to use expression inside switch case statement with JavaScript.

In this article, we’ll look at how to use expression inside switch case statement with JavaScript.

How to use expression inside switch case statement with JavaScript?

To use expression inside switch case statement with JavaScript, we use switch with true.

For instance, we write

switch (true) {
  case amount >= 7500 && amount < 10000:
    // ...
    break;
  case amount >= 10000 && amount < 15000:
    // ...
    break;
  // ...
}

to use switch with true.

Then we can use any boolean expression with case to run code according to the amount value.

Conclusion

To use expression inside switch case statement with JavaScript, we use switch with true.