Get to know MDN better
此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
加法(+)運算子會產生數值運算元的總和或字串串接的結果。
+ 運算子被重載用於兩種不同的操作:數值加法與字串串接。在運算時,它會先將兩個運算元強制轉換為原始型別。接著,會檢查這兩個運算元的型別:
字串串接常被認為等同於模板字面值或 String.prototype.concat(),但並非如此。加法會將運算式強制轉換為原始型別,此過程會優先呼叫 valueOf();而模板字面值與 concat() 則會將運算式強制轉換為字串,優先呼叫 toString()。如果運算式有 [Symbol.toPrimitive]() 方法,字串串接時會以「default」作為提示呼叫,而模板字面值則使用「string」。這對於具有不同字串與原始值表示的物件很重要,例如 Temporal,其物件的 valueOf() 方法會拋出全部例外。
建議你不要使用 "" + x 來進行字串強制轉換。
其他非字串、非 BigInt 的值會被轉換為數字:
你不能在加法中混用 BigInt 與數字運算元。
若要將 BigInt 與非 BigInt 進行加法,請強制轉換任一運算元:
如果其中一個運算元是字串,另一個會被轉換為字串並進行串接:
| ECMAScript® 2027 Language Specification # sec-addition-operator-plus |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2026年2月2日 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.