aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/operators/unary_negation/index.html
blob: 2170b92c534338e25121fb974af876d5b17ac653 (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
---
title: Unary negation (-)
slug: Web/JavaScript/Reference/Operators/Unary_negation
translation_of: Web/JavaScript/Reference/Operators/Unary_negation
---
<div>{{jsSidebar("Operators")}}</div>

<p>The unary negation operator (<code>-</code>) precedes its operand and negates it.</p>

<div>{{EmbedInteractiveExample("pages/js/expressions-unary-negation.html")}}</div>

<div class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.</div>

<h2 id="语法">语法</h2>

<pre class="syntaxbox notranslate"><strong>Operator:</strong> -<var>x</var>
</pre>

<h2 id="Examples">Examples</h2>

<h3 id="Negating_numbers">Negating numbers</h3>

<pre class="brush: js notranslate">const x = 3;
const y = -x;

// y = -3
// x = 3
</pre>

<h3 id="Negating_non-numbers">Negating non-numbers</h3>

<p>The unary negation operator can convert a non-number into a number.</p>

<pre class="brush: js notranslate">const x = "4";
const y = -x;

// y = -4
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-unary-minus-operator', 'Unary negation operator')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>



<p>{{Compat("javascript.operators.unary_negation")}}</p>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Addition">Addition operator</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Subtraction">Subtraction operator</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Division">Division operator</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Multiplication">Multiplication operator</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Remainder">Remainder operator</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Exponentiation">Exponentiation operator</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Increment">Increment operator</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Decrement">Decrement operator</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Unary_plus">Unary plus operator</a></li>
</ul>