相加運算子(+)

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

相加運算子(+)用來處理數字的相加或是進行字串的串聯。

嘗試一下

語法

js
x + y

範例

數字相加

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"

規範

Specification
ECMAScript Language Specification
# sec-addition-operator-plus

瀏覽器相容性

BCD tables only load in the browser

參見