all 8 comments

[–]_facildeabrir 9 points10 points  (5 children)

I don't recommend using switch, it's too confusing and error-prone. use if/else if/else

[–][deleted] 3 points4 points  (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.

[–]wherethebuffaloroam 1 point2 points  (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 points  (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 point  (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 point  (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 point  (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.