That's not the equivalent if-statement. The switch
statement is specified to use the strict equality ===
operator.
A switch statement doesn't work when the equivalent if statement does
Pergunta
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
.
Solução
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow