반응형
switch문은 다중 if 문을 대체 할 수 있음 .. 예를들어..
const a = ;
if (a ===1){
console.log(1);
}else if (a ===2){
console.log(2);
}else if (a ===3){
console.log(3);
}else{
console.log(0);
}
의 경우
const a = 1;
switch(a){
case 1:
console.log(0);
break;
case 2:
console.log(0);
break;
case 3:
console.log(0);
break;
default :
console.log(0);
break;
}
... 로 작성이 가능하다 .
각 case 에 break 추가해서 아래의 case가 실행이 되지 않게 한다. break가 없으면 case 가 일치하는 아래의 모든 case를 실행한다.
default는 case에 맞는 조건이 없을 때 실행된다. if 문에서 마지막 else{} 문에 해당한다.
반응형
'WEB front-end > Javascript' 카테고리의 다른 글
호이스팅(hoisting) (0) | 2022.05.26 |
---|---|
arrow function (0) | 2022.05.17 |
반복문 (0) | 2022.05.17 |
3항 연산자... (0) | 2022.05.17 |
Javascript 간편정리 (0) | 2022.05.16 |
댓글