[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Operators/Addition [Back]  [Original]

+ - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

+

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 20157.

+

In this article

console.log(2 + 2);
// 4

console.log(2 + true);
// 3

console.log(" " + "");
//  

console.log(2001 + "");
// 2001

js
x + y

+

String.prototype.concat() valueOf() concat() toString() [Symbol.toPrimitive]() defaultstring Temporal valueOf()

js
const t = Temporal.Now.instant();
"" + t; //  TypeError
`${t}`; // '2022-07-31T04:48:56.113918308Z'
"".concat(t); // '2022-07-31T04:48:56.113918308Z'

"" + x

js
1 + 2; // 3

BigInt

js
true + 1; // 2
false + false; // 0

BigInt

js
1n + 2n; // 3n

BigInt

js
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

js
1n + BigInt(2); // 3n
Number(1n) + 2; // 3

js
"foo" + "bar"; // foobar
5 + "foo"; // 5foo
"foo" + false; // foofalse
"2" + 2; // 22

Specification
ECMAScript 2027 LanguageSpecification
# sec-addition-operator-plus


Web Proxy Viewer  |  New URL  |  Original Page