سؤال

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