ëí기 (+)
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ì.
ëí기 ì°ì°ì(+)ë ì«ì í¼ì°ì°ìì í©ì 구íê±°ë, 문ìì´ì ìë¡ ì°ê²°í©ëë¤.
ìëí´ ë³´ê¸°
console.log(2 + 2);
// Expected output: 4
console.log(2 + true);
// Expected output: 3
console.log("hello " + "everyone");
// Expected output: "hello everyone"
console.log(2001 + ": A Space Odyssey");
// Expected output: "2001: A Space Odyssey"
구문
x + y
ì¤ëª
+ ì°ì°ìë ì«ì ë§ì
ê³¼ 문ìì´ ì°ê²° ë ê°ì§ ìì
ì ìíí©ëë¤. ê³ì° ìì 먼ì ë ê°ì í¼ì°ì°ì를 ìì íì
ì¼ë¡ ë³íí©ëë¤. ê·¸ íì, ë í¼ì°ì°ìì íì
ì íì¸í©ëë¤.
- ë§ì½ í쪽ì í¼ì°ì°ìê° ë¬¸ìì´ì¸ ê²½ì°, ëë¨¸ì§ í쪽ì í¼ì°ì°ìë 문ìì´ë¡ ë³íìí¨ ë¤ ì°ê²°í©ëë¤.
- ë§ì½ ë í¼ì°ì°ìê° ëª¨ë BigIntì¸ ê²½ì°, BigInt ë§ì
ì ìíí©ëë¤. ë§ì½ í쪽ì í¼ì°ì°ìê° BigIntì´ì§ë§ ëë¨¸ì§ í쪽ì í¼ì°ì°ìê° BigIntê° ìë ê²½ì°,
TypeErrorê° ë°ìí©ëë¤. - ê·¸ ì¸ì ê²½ì°, ë í¼ì°ì°ì를 모ë ì«ìë¡ ë³íìí¤ê³ , ì«ì ë§ì ì´ ìíë©ëë¤.
ì¢
ì¢
í
í릿 리í°ë´ì´ë String.prototype.concat()ì´ ë¬¸ìì´ ì°ê²°ê³¼ ëì¼íë¤ê³ ìê°íì§ë§, ê·¸ë ì§ ììµëë¤. ëí기 ì°ì°ìë 먼ì valueOf()를 í¸ì¶íì¬ ííìì ìì íì
ì¼ë¡ ë³íí©ëë¤. ë°ë©´ì, í
í릿 리í°ë´ê³¼ concat()ì 먼ì toString()ì í¸ì¶í©ëë¤. ë§ì½ ííìì @@toPrimitive ë©ìëê° ìë ê²½ì°, 문ìì´ ì°ê²°ì "default"를 íí¸ë¡ ì¬ì©íê³ , í
í릿 리í°ë´ì "string"ì ì¬ì©í©ëë¤. ì´ ì°¨ì´ë 문ìì´ê³¼ ìì ííì´ ë¤ë¥¸ ê°ì²´ì ì¤ìí©ëë¤. ì를 ë¤ì´ valueOf() ë©ìëê° ìë¬ë¥¼ ë´ë Temporal ê°ì ê°ì²´ê° ììµëë¤.
const t = Temporal.Now.instant();
"" + t; // Throws TypeError
`${t}`; // '2022-07-31T04:48:56.113918308Z'
"".concat(t); // '2022-07-31T04:48:56.113918308Z'
문ìì´ ë³íì ìí´ "" + x를 ì¬ì©íë ê²ì ì¢ì§ ììµëë¤.
ìì
>Number ë§ì
// Number + Number -> ë§ì
1 + 2; // 3
// Boolean + Number -> ë§ì
true + 1; // 2
// Boolean + Boolean -> ë§ì
false + false; // 0
BigInt ë§ì
// BigInt + BigInt -> ë§ì
1n + 2n; // 3n
// BigInt + Number -> TypeError ë°ì
1n + 2; // TypeError: Cannot mix BigInt and other types, use explicit conversions
// BigIntì non-BigInt를 ëí기 ìí´ì , í¼ì°ì°ì ì¤ íë를 ë³ííììì¤.
1n + BigInt(2); // 3n
Number(1n) + 2; // 3
문ìì´ ì°ê²°
// String + String -> ì°ê²°
"foo" + "bar"; // "foobar"
// Number + String -> ì°ê²°
5 + "foo"; // "5foo"
// String + Boolean -> ì°ê²°
"foo" + false; // "foofalse"
ëª ì¸
| Specification |
|---|
| ECMAScript® 2027 Language Specification > # sec-addition-operator-plus > |