aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/operators/unary_plus/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/javascript/reference/operators/unary_plus/index.html')
-rw-r--r--files/zh-cn/web/javascript/reference/operators/unary_plus/index.html75
1 files changed, 75 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/operators/unary_plus/index.html b/files/zh-cn/web/javascript/reference/operators/unary_plus/index.html
new file mode 100644
index 0000000000..9f3b818df9
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/operators/unary_plus/index.html
@@ -0,0 +1,75 @@
+---
+title: Unary plus (+)
+slug: Web/JavaScript/Reference/Operators/Unary_plus
+translation_of: Web/JavaScript/Reference/Operators/Unary_plus
+---
+<div>{{jsSidebar("Operators")}}</div>
+
+<p>The unary plus operator (<code>+</code>) precedes its operand and evaluates to its operand but attempts to convert it into a number, if it isn't already.</p>
+
+<div>{{EmbedInteractiveExample("pages/js/expressions-unary-plus.html", "taller")}}</div>
+
+
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox notranslate"><strong>Operator:</strong> +<var>x</var>
+</pre>
+
+<h2 id="Description">Description</h2>
+
+<p>Although unary negation (<code>-</code>) also can convert non-numbers, unary plus is the fastest and preferred way of converting something into a number, because it does not perform any other operations on the number. It can convert string representations of integers and floats, as well as the non-string values <code>true</code>, <code>false</code>, and <code>null</code>. Integers in both decimal and hexadecimal (<code>0x</code>-prefixed) formats are supported. Negative numbers are supported (though not for hex). Using the operator on BigInt values throws a TypeError. If it cannot parse a particular value, it will evaluate to {{jsxref("NaN")}}.</p>
+
+<h2 id="Examples">Examples</h2>
+
+<h3 id="Usage_with_numbers">Usage with numbers</h3>
+
+<pre class="brush: js notranslate">const x = 1;
+const y = -1;
+
+console.log(+x);
+// 1
+console.log(+y);
+// -1</pre>
+
+<h3 id="Usage_with_non-numbers">Usage with non-numbers</h3>
+
+<pre class="brush: js notranslate">+true // 1
++false // 0
++null // 0
++function(val){ return val } // NaN
++1n // throws TypeError: Cannot convert BigInt value to number
+</pre>
+
+<h2 id="Specifications">Specifications</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-unary-plus-operator', 'Unary plus operator')}}</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility">Browser compatibility</h2>
+
+
+
+<p>{{Compat("javascript.operators.unary_plus")}}</p>
+
+<h2 id="See_also">See also</h2>
+
+<ul>
+ <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Addition">Addition operator</a></li>
+ <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>
+</ul>