0

I've researched this is a little bit. Still curious as to why switch doesnt work with number values. Or doesn't work "as expected".

let vw = window.innerWidth;

switch(vw) {
  case vw >= 1500:
    console.log('Large Desktop');
    break;
  case vw >= 1024 && vw <= 1499:
    console.log('Desktop');
    break;
  default:
    console.log('Mobile || Tablet');
    break;
}

I'm aware that I could simply use an if/else statement. Just personally like to use switch statements with conditionals that typically have more than 3 argument checks.

Thanks in advance.

Josh
  • 1,455
  • 19
  • 33
  • you could have a look here: https://stackoverflow.com/questions/58752462/javascript-function-not-passing-my-parameters-to-other-function/58752619#58752619 – Nina Scholz Nov 15 '19 at 17:04

1 Answers1

2

It is working as expected: cases compare to the value you input to the switch statement. Window width is a number. So you're comparing a number to true/false.

Switch on true if you want to do what you're doing.

DanCouper
  • 850
  • 6
  • 15