Javascript

switch 조건문

아이티프로 2023. 1. 24.
반응형

switch 문은 식의 값을 여러 개의 경우와 비교하고 일치하는 경우 블록 내의 코드가 실행된다.

각 블록의 하단에는 break문을 사용해 다른 블록으로 실행을 막아야 한다.

let day = 'Monday';
switch (day) {
    case 'Monday':
        console.log('Today is Monday');
        break;
    case 'Tuesday':
        console.log('Today is Tuesday');
        break;
    case 'Wednesday':
        console.log('Today is Wednesday');
        break;
    default:
        console.log('Today is another day');
}
let x = 3;
switch(true){
  case x > 0 && x < 5:
	console.log("x is between 0 and 5");
	break;
  case x > 5 && x < 10:
    console.log("x is between 5 and 10");    
    break;
  default:
    console.log("x is not in the range 0 to 10")
}

 

반응형

'Javascript' 카테고리의 다른 글

Math , 난수  (0) 2023.01.24
if else 조건문  (0) 2023.01.24
for do while 반복문  (0) 2023.01.24
Map set Iterating  (0) 2023.01.24
날짜 객체 new Date()  (0) 2023.01.24

댓글