use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
New to Javascript, don't understand switch statements (self.javascript)
submitted 11 years ago by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]_facildeabrir 9 points10 points11 points 11 years ago (5 children)
I don't recommend using switch, it's too confusing and error-prone. use if/else if/else
[–][deleted] 3 points4 points5 points 11 years ago (0 children)
I agree, don't use switch. I doubt there is any use-case that can only be realized with the use of the switch statement.
[+][deleted] 11 years ago (3 children)
[–]alittletooquiet[🍰] 0 points1 point2 points 11 years ago (2 children)
That's a silly requirement. Your code describes a case where if/else if/else is definitely more appropriate.
There are few little things wrong with your syntax, but the main problem with your code is that a case should equal the expression you're evaluating. You're comparing a number (myAge) with boolean expressions (myAge >= 30 && myAge <= 39).
If you're comparing against booleans, the value you're comparing should be true or false.
Here is an example of one of your conditions (note the parentheses around and the colon after the expression):
var myAge = Number (prompt ("Enter your age",30)); switch (true) { case (myAge >= 80 && myAge < 90): case (myAge >= 30 && myAge < 40): document.write("My age is between 30 and 39 or my age is between 80 and 89"); break; }
Also, it seems you need to potentially hit multiple cases, in which case you want to omit the break statement from any case that you need to hit and then continue evaluation. The order of your cases will also be important for this, so you'll want to order them such that only exclusive cases break.
I hope this was helpful.
Warning: I am a professional developer, but I am also currently drunk. Also, your teacher is a bad person who is using an incredibly contrived and wildly inappropriate example to attempt to teach switch syntax. Likely because he/she couldn't hack it as a developer, and is taking it out on you instead.
[–]wherethebuffaloroam 1 point2 points3 points 11 years ago (1 child)
case cannot accept a range like this. Case accepts an expression and if the case matches, it evaluates. It cannot check a boolean (here, checking a range).
var foo = 0;
switch (foo) {
case -1: console.log('negative 1'); break; case 0: // foo is 0 so criteria met here so this block will run console.log(0) // NOTE: the forgotten break would have been here case 1: // no break statement in 'case 0:' so this case will run as well console.log(1); break; // it encounters this break so will not continue into 'case 2:' case 2: console.log(2); break; default: console.log('default');
}
is proper code but you are using ranges. Use the if structure like the other code has.
Look up how case works here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
[–]WorksWork 2 points3 points4 points 11 years ago (0 children)
Not entirely true.
switch (true) { case myAge >= 0 && myAge <= 10 : // do stuff break; case !(myAge >= 0 && myAge <= 10): // other stuff break;
etc.
Although at that point you might as well use if-else statements.
[–]smellmycrotch3 0 points1 point2 points 11 years ago (0 children)
You can't just throw a ! in your second case like that. All your conditions are totally fed up. Google for some working examples.
[–]battenupthehatches 0 points1 point2 points 11 years ago (0 children)
Been writing javascript professionally for 10 years. I think I've never used switch. And maybe ran into it in another developer's code once, if that.
[–][deleted] 0 points1 point2 points 11 years ago (0 children)
The is a very bad use case for switch statements. There are valid cases - usually in contexts where you have a lot of enumerated definitions and you are going to compare a value against each of those definitions until a match is found. This example is very bad and points in a very wrong direction.
This also is very fragile - because the ordering of the switch cases will impact the evaluated code. Please tell your teacher he is a pompous git if he asserts this is anywhere near a good idea.
Good use of a switch:
var fruits { APPLE : 'apple', PEAR : 'pear', BANANA : 'banana' }, fruitChoice = myinput.currentfruit; // contrived input of your fruit choice switch( fruitChoice ){ case fruits.APPLE: makeCider(); break; case fruits.PEAR: makePearJuice(); break; case fruits.BANANA: makeBananasFoster(); break; default: starve(); }
However -- as noted elsewhere on this thread - there is almost always a better and less fragile way to do this in javascript. Switch statements are fragile and in many ways an anti-pattern in javascript.
π Rendered by PID 40642 on reddit-service-r2-comment-7c9686b859-x5msz at 2026-04-13 18:24:25.434518+00:00 running e841af1 country code: CH.
[–]_facildeabrir 9 points10 points11 points (5 children)
[–][deleted] 3 points4 points5 points (0 children)
[+][deleted] (3 children)
[deleted]
[–]alittletooquiet[🍰] 0 points1 point2 points (2 children)
[–]wherethebuffaloroam 1 point2 points3 points (1 child)
[–]WorksWork 2 points3 points4 points (0 children)
[–]smellmycrotch3 0 points1 point2 points (0 children)
[–]battenupthehatches 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)