| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Operators/Addition | [Back] [Original] |
Get to know MDN better
This feature is well established and works across many devices and browser versions. Its been available across browsers since 20157.
+
console.log(2 + 2);
// 4
console.log(2 + true);
// 3
console.log(" " + "");
//
console.log(2001 + "");
// 2001
x + y
String.prototype.concat() valueOf() concat() toString() [Symbol.toPrimitive]() defaultstring Temporal valueOf()
const t = Temporal.Now.instant();
"" + t; // TypeError
`${t}`; // '2022-07-31T04:48:56.113918308Z'
"".concat(t); // '2022-07-31T04:48:56.113918308Z'
1 + 2; // 3
BigInt
true + 1; // 2
false + false; // 0
1n + 2n; // 3n
BigInt
1n + 2; // TypeError: Cannot mix BigInt and other types, use explicit conversions
2 + 1n; // TypeError: Cannot mix BigInt and other types, use explicit conversions
"1" + 2n; // TypeError: Cannot mix BigInt and other types, use explicit conversions
BigInt BigInt
1n + BigInt(2); // 3n
Number(1n) + 2; // 3
"foo" + "bar"; // foobar
5 + "foo"; // 5foo
"foo" + false; // foofalse
"2" + 2; // 22
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-addition-operator-plus |
This page was last modified on 202622 by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |