--- title: 相加运算符 (+) slug: Web/JavaScript/Reference/Operators/Addition translation_of: Web/JavaScript/Reference/Operators/Addition original_slug: Web/JavaScript/Reference/Operators/相加 --- <div>{{jsSidebar("相加运算符")}}</div> <p>相加运算符 (<code>+</code>) 用于对两个操作数进行相加运算,如果操作数为字符串则该运算符将两个操作数连接成一个字符串。</p> <div>{{EmbedInteractiveExample("pages/js/expressions-addition.html")}}</div> <div></div> <h2 id="语法">语法</h2> <pre class="syntaxbox notranslate"><strong>表达式:</strong> <var>x</var> + <var>y</var> </pre> <h2 id="示例">示例</h2> <h3 id="数字的相加运算">数字的相加运算</h3> <pre class="brush: js notranslate">// Number + Number -> addition 1 + 2 // 3 // Boolean + Number -> addition true + 1 // 2 // Boolean + Boolean -> addition false + false // 0 </pre> <h3 id="字符串相加运算">字符串相加运算</h3> <pre class="brush: js notranslate">// String + String -> concatenation 'foo' + 'bar' // "foobar" // Number + String -> concatenation 5 + 'foo' // "5foo" // String + Boolean -> concatenation 'foo' + false // "foofalse"</pre> <p>注: '+'两侧只要有一侧是字符串,另一侧的数字则会自动转换成字符串,因为其中存在隐式转换</p> <h2 id="规范">规范</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">规范</th> </tr> <tr> <td>{{SpecName('ESDraft', '#sec-addition-operator-plus', 'Addition operator')}}</td> </tr> </tbody> </table> <h2 id="浏览器兼容性">浏览器兼容性</h2> <p>{{Compat("javascript.operators.addition")}}</p> <h2 id="参考">参考</h2> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Subtraction">Subtraction operator</a></li> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Division">Division operator</a></li> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Multiplication">Multiplication operator</a></li> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder">Remainder operator</a></li> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Exponentiation">Exponentiation operator</a></li> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Increment">Increment operator</a></li> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Decrement">Decrement operator</a></li> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unary_negation">Unary negation operator</a></li> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unary_plus">Unary plus operator</a></li> </ul>