jeudi 9 septembre 2021

Condition in switch's case is ignored

I'm trying to use a switch statement as follow. But even if status is 301, the case case (status > 200 && status < 400) is ignored and the result is the "default" case. What am I doing wrong? Is it not possibile to use conditions in case statements?

status = 301;
switch (status) {
  case 0:
  case 200:
    console.log('200');
    break;
  case (status > 200 && status < 400):
    console.log('200-400');
    break;
  case 404:
    console.log('404');
    break;
  default:
    console.log('Si è verificato un errore irreversibile.');
}

At the end I solved with a simple if, but I would like to know why this doesn't work.

Aucun commentaire:

Enregistrer un commentaire