return
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2015ë 7ì.
return ëª
ë ¹ë¬¸ì í¨ì ì¤íì ì¢
ë£íê³ , 주ì´ì§ ê°ì í¨ì í¸ì¶ ì§ì ì¼ë¡ ë°íí©ëë¤.
ìëí´ ë³´ê¸°
function getRectArea(width, height) {
if (width > 0 && height > 0) {
return width * height;
}
return 0;
}
console.log(getRectArea(3, 4));
// Expected output: 12
console.log(getRectArea(-3, 4));
// Expected output: 0
구문
return [[expression]];
expression-
ë°íí ê°ì¼ë¡ ì¬ì©í ííì. ìëµí ê²½ì°
undefined를 ëì ë°íí©ëë¤.
ì¤ëª
í¨ì 본문ìì return ëª
ë ¹ë¬¸ì ëë¬íë©´ í¨ìì ì¤íì ê·¸ ì§ì ìì ì¤ë¨ë©ëë¤. ê°ì ì ê³µí ê²½ì° í¨ì를 í¸ì¶í ê³³ì ê·¸ ê°ì ë°íí©ëë¤. ì를 ë¤ì´, ë¤ì í¨ìë ì«ì 매ê°ë³ì xì ì ê³±ì ë°íí©ëë¤.
function square(x) {
return x * x;
}
var demo = square(3);
// demoë 9
ê°ì ëª
ìíì§ ìì¼ë©´ ëì undefined를 ë°íí©ëë¤.
ë¤ì return ëª
ë ¹ë¬¸ 모ë í¨ì ì¤íì ëìµëë¤.
return;
return true;
return false;
return x;
return x + y / 3;
ìë ì¸ë¯¸ì½ë¡ ì½ì
return ëª
ë ¹ë¬¸ì ìë ì¸ë¯¸ì½ë¡ ì½ì
(ASI)ì ìí¥ì ë°ìµëë¤. return í¤ìëì ííì ì¬ì´ìë ì¤ë°ê¿ 문ìê° ì¬ ì ììµëë¤.
return
a + b;
ì ì½ëë ASIë¡ ì¸í´ ìëì²ë¼ ì²ë¦¬ë©ëë¤.
return;
a + b;
ì½ìì´ "unreachable code after return statement" ê²½ê³ ë¥¼ ì¶ë ¥í ê²ì ëë¤.
문ì 를 í´ê²°íë ¤ë©´ ê´í¸ë¥¼ ì¬ì©í´ ASI를 ë°©ì§í´ì¼ í©ëë¤.
return a + b;
ìì
>í¨ì ì¤ë¨
í¨ìë returnì í¸ì¶íë ì§ì ìì ì¦ì ì¤íì ë©ì¶¥ëë¤.
function counter() {
for (var count = 1; ; count++) {
// 무í ë°ë³µ
console.log(count + "A"); // 5ê¹ì§
if (count === 5) {
return;
}
console.log(count + "B"); // 4ê¹ì§
}
console.log(count + "C"); // ì ë ëíëì§ ìì
}
counter();
// ì¶ë ¥:
// 1A
// 1B
// 2A
// 2B
// 3A
// 3B
// 4A
// 4B
// 5A
í¨ì ë°íí기
í´ë¡ì ì ëí´ìë ë ììë³´ì¸ì.
function magic(x) {
return function calc(x) {
return x * 42;
};
}
var answer = magic();
answer(1337); // 56154
ëª ì¸ì
| Specification |
|---|
| ECMAScript® 2027 Language Specification > # sec-return-statement > |