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

+ - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

+

20157

+

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"

js
x + y

+

String.prototype.concat() valueOf() concat() toString() [Symbol.toPrimitive]() "default" hint "string" 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
// Number + Number -> addition
1 + 2; // 3

// Boolean + Number -> addition
true + 1; // 2

// Boolean + Boolean -> addition
false + false; // 0

js
// 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