Switch Case Statement in JavaScript

 Can I use a case/switch statement with two variables?

Last updated:7/19/2021 5:31:36 AM

1 Answers

Ethan Karla
Ethan Karla

Yes , you can use a two variables in a switch case statement.

Below example can describe you how to use this condition in you javascript code.

<!DOCTYPE html>

<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
    <meta charset='utf-8' />
    <title>Switch Case Statement</title>
    <script>
        var color = 'blue'; var size = 'small';
            switch (true) {
                case ( color == 'blue' && size == 'small'):
                    document.write('blue and small');
                    break;
                case ( color == 'red' && size == 'large'):
                    document.write('red and large');
                    break;
                default:
                    document.write('nothing');
                    break;
            }
        ;
    </script>
</head>
<body align='center'>
</body>
</html>

Hope this information has cleared your confusion.

Happy Coding!

Answer