| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Addition | [Back] [Original] |
Get to know MDN better
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
String.prototype.concat() valueOf() concat() toString() [Symbol.toPrimitive]() "default" hint "string" Temporal valueOf()
const t = Temporal.Now.instant();
"" + t; // TypeError
`${t}`; // '2022-07-31T04:48:56.113918308Z'
"".concat(t); // '2022-07-31T04:48:56.113918308Z'
// Number + Number -> addition
1 + 2; // 3
// Boolean + Number -> addition
true + 1; // 2
// Boolean + Boolean -> addition
false + false; // 0
// String + String -> concatenation
"foo" + "bar"; // "foobar"
// Number + String -> concatenation
5 + "foo"; // "5foo"
// String + Boolean -> concatenation
"foo" + false; // "foofalse"
// String + Number -> concatenation
"2" + 2; // "22"
| ECMAScript 2027 LanguageSpecification # sec-addition-operator-plus |
| Web Proxy Viewer | New URL | Original Page |