Categories
JavaScript Answers

How to use a case and switch statement with two variables with JavaScript?

Spread the love

Sometimes, we want to use a case and switch statement with two variables with JavaScript.

In this article, we’ll look at how to use a case and switch statement with two variables with JavaScript.

How to use a case and switch statement with two variables with JavaScript?

To use a case and switch statement with two variables with JavaScript, we use switch with true.

For instance, we write

switch (true) {
  case var1 === true && var2 === true:
    //...
    break;
  case var1 === false && var2 === false:
    //...
    break;

  default:
}

to use switch with true.

Then the case statements can check for any boolean expression.

Conclusion

To use a case and switch statement with two variables with JavaScript, we use switch with true.

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 *