viery365
viery365

Reputation: 965

Switch statement and type coercion

I read in the 'Professional JavaScript for Web Developers' by Nicholas Zakas in p.78 of 3rd edition (last one I think):

The switch statement compares values using the identically equal operator, so no type coercion occurs (for example the string "10" is not equal to the number 10).

I made up some simple switch statement just to confirm and the result was different:

var num = "9";

switch (true) {
    case num < 0:
        alert("less than 0");
        break;

    case num >= 0 && num <10:
        alert("between 0 and 10");
        break;

    default:
        alert("False");

} 

https://jsfiddle.net/pbxyvjyf/

So, type coercion is done: the alert("between 0 and 10") is chosen. Have the rules changed or am I doing something wrong?

Upvotes: 7

Views: 968

Answers (5)

ManoS
ManoS

Reputation: 42

Not an expert of javascript but I think that the rule that the author mentioned is valid in this case, for example:

   var num = "9";

    switch(num){

        case 9:
            alert("Yes the number is equal to: 9");
            break;
        default:
            alert("False");

    }

If you try to run this code you will see that the second alert will appear.

Hope that I helped you.. :)

Upvotes: 1

JLRishe
JLRishe

Reputation: 101728

What that line from the documentation means is that no type coercion is used in comparing the switch value to each of the case values.

If the case expression itself is an expression that would involve type coercion, then there will obviously be type coercion taking place. num >= 0 && num <10 evaluates to true, but the engine will use the "identically equal operator" in comparing that true against the true at the top of the switch statement. Since true is identically equal to true, the control flow goes into that case.

Upvotes: 3

Robert Moskal
Robert Moskal

Reputation: 22553

You really aren't using the switch statement as intended. There should be an expression that's evaluated in the switch and the cases check for possible values. This is from one of the canonical examples:

switch (new Date().getDay()) {
    case 0:
        day = "Sunday";
        break;
    case 1:
        day = "Monday";
        break;
    case 2:
        day = "Tuesday";
        break;
    case 3:
        day = "Wednesday";
        break;
    case 4:
        day = "Thursday";
        break;
    case 5:
        day = "Friday";
        break;
    case 6:
        day = "Saturday";
}

You are using the switch statement more like a bunch of ifs.

Upvotes: 3

WalksAway
WalksAway

Reputation: 2829

your case statements return a boolean so the type is correct

num >= 0 && num <10 - returns true or false

but if i would do this

switch (1) {
    case "1":
        console.log("never get here");
        break;

    case 1:
        console.log("but this works");
        break;

} 

Upvotes: 10

Gerardo Furtado
Gerardo Furtado

Reputation: 102198

This will return false:

var num = "9";

switch (num) {
  case num < 0:
    alert("less than 0");
    break;

  case num >= 0 && num <10:
    alert("between 0 and 10");
    break;

  default:
    alert("False");

}

Therefore, the author is correct.

Upvotes: 7

Related Questions