简体   繁体   中英

Javascript case (true) throwing Unexpected identifier

When I run the code below on Chrome, the console shows an Unexpected identifier exception.

var a = true;
var b = false;

switch (true) {
  a:
    window.alert('test A');
    break;
  b:
    window.alert('test B');
    break;
  default:
    window.alert('test C');
}

I tried to run the code directly on console to guarantee the error is not caused by other line on my script, but I'm still receiving the exception.

I also looked for an answer through Google, but didn't found any answer for this strange behaviour.

Thanks.

you can't write b: you must write case b:

var a = true;
var b = false;

switch (true) {
  case a:
    window.alert('test A');
    break;
  case b:
    window.alert('test B');
    break;
  default:
    window.alert('test C');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM