我有一个 switch 语句,它正在通过两种情况,而不是我所期望的那种情况:
let name = 'John';
switch (name)
{
case 'john' :
alert('Condition 1 is true.');
case 'John' :
alert('Condition 2 is true');
case 'JOHN' :
alert('Condition 3 is true');
}
我得到的结果:
条件 2 为真
条件 3 为真
为什么我得到这个结果,我不明白?
你需要case
用一个break
语句结束每一个:
let name = 'John';
switch (name)
{
case 'john' :
alert('Condition 1 is true.');
break;
case 'John' :
alert('Condition 2 is true');
break;
case 'JOHN' :
alert('Condition 3 is true');
break;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句