質問

switch(type)
{
    case 'home':
         console.log('home switch');
         break;
}

The above code does not write out to the console, neither does the following:

switch(type)
{
     case "home":
         console.log('home switch');
         break;
}

The following, however, does:

if (type == 'home')
{
     console.log('home if');
}

I don't have a clue why. This isn't a show-stopper, I can use the if statement instead, but I'm genuinely curious as to why this is the case.

NOTE: These statements are a straight replace, nothing else to consider here. No change in scope, no code I'm not mentioning that could be interfering with the value of type.

役に立ちましたか?

解決

That's not the equivalent if-statement. The switch statement is specified to use the strict equality === operator.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top