aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/operators/unary_plus/index.html
blob: 9f3b818df93e8c1a8a92fd88913e527f05bd95b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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>