본문 바로가기
FrontEnd/JavaScript

[ JavaScript ] switch문에서 비교연산자 사용하기

by ウリ김영은 2023. 10. 25.
  • 불가능
var age = prompt("Enter you age");
switch (age) {
    case <13:
        alert("You must be 13 or older to play");
        break;
    case >=13:
        alert("You are old enough to play");
        break;
}
  • 가능
switch (true) {
    case (age < 13):
        alert("You must be 13 or older to play");
        break;
    case (age >= 13):
        alert("You are old enough to play");
        break;
}