Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Switch Statement

Matt Cook
Matt Cook
5,172 Points

Switch Statement JavaScript

When using a switch statement like the video, it seems its just testing whether the case is true or not. I also found you can add multiple lines of cases to evaluate true as well. My question is can you test actual conditions, and what would the syntax look like? //testing multiple conditions switch(value){ case 'cat': case 'goat': case 'dog': console.log('Correct choice'); break; default: console.log('incorrect choice'); break; } //Can a switch statement test if value is > 3 or < 3? let value = 6;

switch (value) { case () break; }

1 Answer

Steven Parker
Steven Parker
229,744 Points

Your first example (cat/goat/dog) looks correct and should work. But you cannot use "switch" for range comparisons. The nature of "switch" is that the cases represent exact matches with the provided value.

To implement range comparisons, try using an "if/else if/else" chain.

Matt Cook
Matt Cook
5,172 Points

Thanks Steven! I normally use if or ternary statements myself, just wanted to make sure I wasn't missing out on a key concept in switch statements.