Baseline Widely available
switch
문ì ííìì íê°íê³ , ê·¸ ê²°ê³¼ê°ê³¼ ì¼ì¹íë case
ì ì ì°¾ì í´ë¹ ì ë¶í° break
문ì ë§ë ëê¹ì§ì 문ì ì¤íí©ëë¤. ì¼ì¹íë case
ê° ìì¼ë©´ default
ì ì´ ì¤íë©ëë¤.
const expr = "Papayas";
switch (expr) {
case "Oranges":
console.log("Oranges are $0.59 a pound.");
break;
case "Mangoes":
case "Papayas":
console.log("Mangoes and papayas are $2.79 a pound.");
// ìì ì¶ë ¥: "Mangoes and papayas are $2.79 a pound."
break;
default:
console.log(`Sorry, we are out of ${expr}.`);
}
구문
switch (expression) {
case caseExpression1:
statements
case caseExpression2:
statements
// â¦
case caseExpressionN:
statements
default:
statements
}
expression
ê° case
ì ê³¼ ëì¡°í ííìì
ëë¤.
caseExpressionN
Optional
expression
ê³¼ ë¹êµëë case
ì ì ê°ì
ëë¤. expression
ì ê°ì´ ì´ë¤ caseExpressionN
ê³¼ ì¼ì¹íë©´, í´ë¹ case
ì ì´íì 첫 ë²ì§¸ 문(statement) ë¶í° switch
문 ë ëë break
문ì ë§ë ëê¹ì§ ì¤íì´ ììë©ëë¤.
default
Optional
기본 ì ì
ëë¤. expression
ì´ ì´ë¤ case
ìë ì¼ì¹íì§ ìì¼ë©´ ì´ ì ì´ ì¤íë©ëë¤. switch
문ìë íëì default
ì ë§ ì¡´ì¬í ì ììµëë¤.
switch
문ì 먼ì 주ì´ì§ ííìì íê°í©ëë¤. ê·¸ ê²°ê³¼ì ëì¼í ê°ì ê°ì§ë case
ì ì ì격í ëë± ë¹êµë¥¼ íµí´ ì°¾ì ë¤, í´ë¹ ì ë¡ ì ì´ íë¦ì ì´ëìì¼ ê·¸ ì ì´íì 문(statement) ë¤ì ì¤íí©ëë¤.
ê° case
ì ì ííìì íìí ëë§ íê°ë©ëë¤. ì¼ì¹íë ì ì´ ì´ë¯¸ ë°ê²¬ëë©´, ì´íì case
ì ííìì í´ì¤ë£¨ë¡ ëë¬íëë¼ë íê°ëì§ ììµëë¤.
switch (undefined) {
case console.log(1):
case console.log(2):
}
// 1ë§ ë¡ê·¸ì ì¶ë ¥
ì¼ì¹íë case
ì ì´ ìì¼ë©´, íë¡ê·¸ë¨ì ì íì ì¼ë¡ ì ìë default
ì ë¡ ì ì´ë¥¼ ì´ëíì¬ ì´í 문(statement) ì ì¤íí©ëë¤. default
ì ì´ ìì¼ë©´ switch
문ì ë¹ ì ¸ëì ë¤ì 문(statement) ë¶í° ì¤íì ê³ìí©ëë¤. default
ì ì ë³´íµ ë§ì§ë§ì ìì¹íì§ë§ ë°ëì ê·¸ë´ íìë ìì¼ë©°, íëë§ ì¬ì©í ì ììµëë¤. ë ì´ìì default
ì ì´ ìì¼ë©´ SyntaxError
ê° ë°ìí©ëë¤.
switch
문 본문 ë´ìì break
문ì ì¬ì©íë©´, í´ë¹ case
ì ì ì¤íì ë§ì¹ í switch
문ì ë¹ ì ¸ëìµëë¤. ì¼ë°ì ì¼ë¡ ì¬ë¬ case
ì ì¬ì´ 문ì¥ì 모ë ì¤íí ë¤ ì¢
ë£í ë ì¬ì©ë©ëë¤.
break
ê° ìì¼ë©´ ì¤íì ë¤ì case
ì ë¡ ì´ì´ì§ë©°, default
ì ê¹ì§ ì§íë ì ììµëë¤. ì´ë¬í ëìì "í´ì¤ë£¨(fall-through)"ë¼ê³ í©ëë¤.
const foo = 0;
switch (foo) {
case -1:
console.log("negative 1");
break;
case 0: // fooì ê°ì´ ì´ ê¸°ì¤ê³¼ ì¼ì¹í©ëë¤; ì¬ê¸°ìë¶í° ì¤íì´ ììë©ëë¤
console.log(0);
// break를 ìììµëë¤! í´ì¤ë£¨ê° ëìë©ëë¤.
case 1: // 'case 0:'ì break ë¬¸ì´ ìì¼ë¯ë¡ ì´ caseë ì¤íë©ëë¤
console.log(1);
break; // break를 ë§ë¬ìµëë¤. 'case 2:'ë¡ ê³ìëì§ ììµëë¤
case 2:
console.log(2);
break;
default:
console.log("default");
}
// 0ê³¼ 1ì´ ë¡ê·¸ì ì¶ë ¥ë©ëë¤
ì ì í ìí©ììë ë¤ë¥¸ ì ì´ íë¦ ë¬¸ì¥ë switch
문ìì ë²ì´ëë í¨ê³¼ê° ììµëë¤. ì를 ë¤ì´ switch
ë¬¸ì´ í¨ì ë´ì í¬í¨ëì´ ìë ê²½ì°, return
문ì í¨ì 본문ì ì¤íì ì¢
ë£íë¯ë¡ switch
문ë ì¢
ë£í©ëë¤. switch
ë¬¸ì´ ë°ë³µë¬¸ ë´ì í¬í¨ëì´ ìë ê²½ì°, continue
문ì switch
문ì ì¤ì§íê³ ë°ë³µë¬¸ì ë¤ì ë°ë³µì¼ë¡ ê±´ëëëë¤.
case
ì default
ì ì ë ì´ë¸(label)ê³¼ ê°ìµëë¤: ì ì´ íë¦ì´ ëë¬ ê°ë¥í ìì¹ë¥¼ ëíë
ëë¤. ê·¸ë¬ë ê·¸ë¤ ìì²´ë ë ì컬 ì¤ì½í를 ìì±íì§ ììµëë¤(ëí ììì ì¤ëª
í ëë¡ ìëì¼ë¡ ë²ì´ëì§ë ììµëë¤). ì를 ë¤ì´:
const action = "say_hello";
switch (action) {
case "say_hello":
const message = "hello";
console.log(message);
break;
case "say_hi":
const message = "hi";
console.log(message);
break;
default:
console.log("Empty action received.");
}
ì´ ìì ë "Uncaught SyntaxError: Identifier 'message' has already been declared" ì¤ë¥ë¥¼ ì¶ë ¥í©ëë¤. 첫 ë²ì§¸ const message = 'hello';
ì ì¸ì´ ë ë²ì§¸ const message = 'hi';
ì ì¸ê³¼ ì¶©ëí기 ë문ì
ëë¤. ì´ë ê°ê° ë³ëì case ì ë´ì ìëë¼ë ë°ìí©ëë¤. ê¶ê·¹ì ì¼ë¡ ì´ë ë const
ì ì¸ ëª¨ë switch
본문ì ìí´ ìì±ë ëì¼í ë¸ë¡ ì¤ì½í ë´ì ì기 ë문ì
ëë¤.
ì´ë¥¼ í´ê²°íë ¤ë©´ case
ì ìì let
ì´ë const
ì ì¸ì ì¬ì©í´ì¼ í ëë§ë¤ ë¸ë¡ì¼ë¡ 묶ì´ì¼ í©ëë¤.
const action = "say_hello";
switch (action) {
case "say_hello": {
const message = "hello";
console.log(message);
break;
}
case "say_hi": {
const message = "hi";
console.log(message);
break;
}
default: {
console.log("Empty action received.");
}
}
ì´ ì½ëë ì´ì ì¤ë¥ ìì´ ì½ìì hello
를 ì¶ë ¥í©ëë¤.
ë¤ì ìì ìì, expr
ì´ Bananas
ë¡ íê°ëë©´ íë¡ê·¸ë¨ì ê°ì case 'Bananas'
ì ì¼ì¹ìí¤ê³ ê´ë ¨ 문(statement) ì ì¤íí©ëë¤. break
를 ë§ëë©´ íë¡ê·¸ë¨ì switch
ìì ë²ì´ë switch
ë¤ìì 문(statement) ì ì¤íí©ëë¤. break
ê° ìëµëìë¤ë©´ case 'Cherries'
ì 문(statement) ë ì¤íë©ëë¤.
switch (expr) {
case "Oranges":
console.log("Oranges are $0.59 a pound.");
break;
case "Apples":
console.log("Apples are $0.32 a pound.");
break;
case "Bananas":
console.log("Bananas are $0.48 a pound.");
break;
case "Cherries":
console.log("Cherries are $3.00 a pound.");
break;
case "Mangoes":
case "Papayas":
console.log("Mangoes and papayas are $2.79 a pound.");
break;
default:
console.log(`Sorry, we are out of ${expr}.`);
}
console.log("Is there anything else you'd like?");
ë case ì ì¬ì´ì default ì ë기
ì¼ì¹íë íëª©ì´ ìì¼ë©´, default
ì ë¶í° ì¤íì´ ììëê³ ê·¸ ì´íì 모ë 문(statement) ì´ ì¤íë©ëë¤.
const foo = 5;
switch (foo) {
case 2:
console.log(2);
break; // ì´ break를 ë§ë¬ì¼ë¯ë¡ 'default:'ë¡ ê³ìëì§ ììµëë¤
default:
console.log("default");
// í´ì¤ë£¨
case 1:
console.log("1");
}
default
를 ë¤ë¥¸ 모ë case
ì ìì ëì´ë ìëí©ëë¤.
ì´ ë°©ë²ì case
ì ìëì break
ê° ìì ê²½ì°, í´ë¹ case
ê° ê¸°ì¤ì 충족íëì§ ì¬ë¶ì ê´ê³ìì´ ì¤íì´ ë¤ì case
ì ë¡ ê³ìëë¤ë ì¬ì¤ì íì©í©ëë¤.
ë¤ìì ë¤ ê°ì§ ë¤ë¥¸ ê°ì´ ì íí ëì¼í ìì
ì ìííë ë¨ì¼ ëì ìì°¨ case
문ì ìì
ëë¤.
const Animal = "Giraffe";
switch (Animal) {
case "Cow":
case "Giraffe":
case "Dog":
case "Pig":
console.log("This animal is not extinct.");
break;
case "Dinosaur":
default:
console.log("This animal is extinct.");
}
ë¤ìì ì ê³µë ì ìì ë°ë¼ ë¤ë¥¸ ì¶ë ¥ì ë°ì ì ìë ë¤ì¤ ëì ìì°¨ case
ì ì ìì
ëë¤. ì´ë case
ì ì ë°°ì¹í ììëë¡ ìííë©° ë°ëì ì«ì ììëë¡ ì ë ¬ëì´ ìì íìë ììµëë¤. JavaScriptììë ì«ìë¿ë§ ìëë¼, 문ìì´ë case
문ì í¨ê» ì¬ì©í ì ììµëë¤.
const foo = 1;
let output = "Output: ";
switch (foo) {
case 0:
output += "So ";
case 1:
output += "What ";
output += "Is ";
case 2:
output += "Your ";
case 3:
output += "Name";
case 4:
output += "?";
console.log(output);
break;
case 5:
output += "!";
console.log(output);
break;
default:
console.log("Please pick a number from 0 to 5!");
}
ì´ ìì ì ì¶ë ¥:
ê° ë¡ê·¸ í ì¤í¸foo
ê° NaN
ì´ê±°ë 1
, 2
, 3
, 4
, 5
, ëë 0
ì´ ìë Please pick a number from 0 to 5! 0
Output: So What Is Your Name? 1
Output: What Is Your Name? 2
Output: Your Name? 3
Output: Name? 4
Output: ? 5
Output: ! if...else ì²´ì¸ì ëì
ê°ë° ì¤ì ì¢
ì¢
if...else
문ì ì°ë¬ì ìì±íë ìì ì ë°ê²¬íê² ë ìë ììµëë¤.
if ("fetch" in globalThis) {
// fetchë¡ ë¦¬ìì¤ ê°ì ¸ì¤ê¸°
} else if ("XMLHttpRequest" in globalThis) {
// XMLHttpRequestë¡ ë¦¬ìì¤ ê°ì ¸ì¤ê¸°
} else {
// ì¼ë¶ ì¬ì©ì ì ì AJAX ë¡ì§ì¼ë¡ 리ìì¤ ê°ì ¸ì¤ê¸°
}
ì´ í¨í´ì ê¼ ===
ë¹êµë§ì ìííë ê²ì ìëì§ë§, switch
구조ë¡ë ì¶©ë¶í ë³íí ì ììµëë¤.
switch (true) {
case "fetch" in globalThis:
// fetchë¡ ë¦¬ìì¤ ê°ì ¸ì¤ê¸°
break;
case "XMLHttpRequest" in globalThis:
// XMLHttpRequestë¡ ë¦¬ìì¤ ê°ì ¸ì¤ê¸°
break;
default:
// ì¼ë¶ ì¬ì©ì ì ì AJAX ë¡ì§ì¼ë¡ 리ìì¤ ê°ì ¸ì¤ê¸°
break;
}
if...else
ì ëìì¼ë¡ìì switch (true)
í¨í´ì í¹í í´ì¤ë£¨ ëìì íì©íë ¤ë ê²½ì°ì ì ì©í©ëë¤.
switch (true) {
case isSquare(shape):
console.log("This shape is a square.");
// í´ì¤ë£¨, ì ì¬ê°íì ì§ì¬ê°íì´ê¸°ë í기 ë문ì
ëë¤!
case isRectangle(shape):
console.log("This shape is a rectangle.");
case isQuadrilateral(shape):
console.log("This shape is a quadrilateral.");
break;
case isCircle(shape):
console.log("This shape is a circle.");
break;
}
ëª
ì¸ì ë¸ë¼ì°ì í¸íì± ê°ì´ 보기
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4