【发布时间】:2021-11-06 05:58:20
【问题描述】:
我正在尝试使用如下的 switch 语句。但即使状态为 301,大小写 case (status > 200 && status < 400) 也会被忽略,结果是“默认”大小写。
我究竟做错了什么?在case语句中不能使用条件吗?
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.');
}
最后我用一个简单的 if 解决了,但我想知道为什么这不起作用。
【问题讨论】:
-
为什么
status > 200 && status < 400(true或false)等于status(可能是一个数字)? -switch (x)case y就像if (x === y)。
标签: javascript if-statement switch-statement