JavaScript 스위치 문
- 이전 페이지 return
- 다음 페이지 throw
- 上一层으로 돌아가기 JavaScript 문 참조 매뉴얼
정의와 사용법
스위치 문은 JavaScript의
switch 문은 표현식을 계산합니다. 그런 다음 표현식의 값이 구조 내 각 case의 값과 비교됩니다. 일치하면 관련 코드 블록을 실행합니다。
switch 문은 break 또는 default 키워드(또는 둘 다)와 함께 사용됩니다. 모두 선택 사항입니다:
break 키워드는 switch 블록에서 벗어나서 블록 내에서 더 이상 코드를 실행하고/또는 경우 테스트를 중단합니다. break를 제외하면 switch 문에서 다음 코드 블록을 실행합니다.
case가 일치하지 않으면, default 키워드가 실행할 코드를 지정합니다. switch 중 하나에만 default 키워드가 있을 수 있습니다. 선택 사항이지만, 예기치 않은 상황을 처리할 수 있기 때문에 권장합니다.
예제
사용자 입력에 따라 코드 블록을 실행합니다:
var text; var fruits = document.getElementById("myInput").value; switch(fruits) { case "Banana": text = "Banana is good!"; break; case "Orange": text = "I am not a fan of orange."; break; case "Apple": text = "How you like them apples?"; break; default: text = "I have never heard of that fruit..."; }
페이지 아래에 더 많은 TIY 예제가 있습니다.
문법
switch(expression) { case n: 코드 블록 break; case n: 코드 블록 break; default: default 코드 블록 }
파라미터 값
파라미터 | 설명 |
---|---|
expression | 필수입니다. 계산할 표현식을 지정합니다. 표현식은 한 번 계산됩니다. 표현식의 값은 구조 내 각 case 태그의 값과 비교됩니다. 일치하면 관련 코드 블록을 실행합니다。 |
기술 세부 사항
JavaScript 버전: | ECMAScript 1 |
---|
更多实例
예제
今天의 weekday 번호를 사용하여 weekday 이름을 계산합니다 (Sunday=0, Monday=1, Tuesday=2, ...):
var day; switch (new Date().getDay()) { case 0: day = "Sunday"; break; case 1: day = "Monday"; break; case 2: day = "Tuesday"; break; case 3: day = "Wednesday"; break; case 4: day = "Thursday"; break; case 5: day = "Friday"; break; case 6: day = "Saturday"; break; default: day = "Unknown Day"; }
예제
If today is neither Saturday nor Sunday, write a default message:
var text; switch (new Date().getDay()) { case 6: text = "Today is Saturday"; break; case 0: text = "Today is Sunday"; break; default: text = "Looking forward to the Weekend"; }
예제
Sometimes you may want different conditions to use the same code, or use the same default value.
Please note that in this example, the same code block is shared among the cases, and the default case does not have to be the last case in the switch block (but, if the default is not the last case in the switch block, remember to end it with a break).
var text; switch (new Date().getDay()) { case 1: case 2: case 3: default: text = "Looking forward to the Weekend"; break; case 4: case 5: text = "Soon it is Weekend"; break; case 0: case 6: text = "It is Weekend"; }
예제
The code block is executed using the switch statement based on the user input from the prompt box:
var text; var favDrink = prompt("What's your favorite cocktail drink?"); switch(favDrink) { case "Martini": text = "Excellent choice! Martini is good for your soul."; break; case "Daiquiri": text = "Daiquiri is my favorite too!"; break; case "Cosmopolitan": text = "Really? Are you sure the Cosmopolitan is your favorite?"; break; default: text = "I have never heard of that one.."; }
브라우저 지원
문장 | 크롬 | IE | 파이어폭스 | 사파리 | 오페라 |
---|---|---|---|---|---|
switch | 지원 | 지원 | 지원 | 지원 | 지원 |
관련 페이지
JavaScript 강의:JavaScript If...Else 문장
JavaScript 강의:JavaScript Switch 문
JavaScript 참조 매뉴얼:JavaScript if/else 문
JavaScript 참조 매뉴얼:JavaScript break 문
- 이전 페이지 return
- 다음 페이지 throw
- 上一层으로 돌아가기 JavaScript 문 참조 매뉴얼