diff options
author | Florian Merz <me@fiji-flo.de> | 2021-02-11 13:12:08 +0100 |
---|---|---|
committer | Florian Merz <me@fiji-flo.de> | 2021-02-11 13:12:08 +0100 |
commit | 43a5cac2eff22c21071800e13bef12af9d3a37d0 (patch) | |
tree | f6e91f8aa958f15bd0b0aabf7b8dfc09063eceda /files/zh-tw/web/javascript/reference/operators/arithmetic_operators | |
parent | 8260a606c143e6b55a467edf017a56bdcd6cba7e (diff) | |
download | translated-content-43a5cac2eff22c21071800e13bef12af9d3a37d0.tar.gz translated-content-43a5cac2eff22c21071800e13bef12af9d3a37d0.tar.bz2 translated-content-43a5cac2eff22c21071800e13bef12af9d3a37d0.zip |
unslug zh-tw: move
Diffstat (limited to 'files/zh-tw/web/javascript/reference/operators/arithmetic_operators')
-rw-r--r-- | files/zh-tw/web/javascript/reference/operators/arithmetic_operators/index.html | 309 |
1 files changed, 0 insertions, 309 deletions
diff --git a/files/zh-tw/web/javascript/reference/operators/arithmetic_operators/index.html b/files/zh-tw/web/javascript/reference/operators/arithmetic_operators/index.html deleted file mode 100644 index 050c362008..0000000000 --- a/files/zh-tw/web/javascript/reference/operators/arithmetic_operators/index.html +++ /dev/null @@ -1,309 +0,0 @@ ---- -title: 算術運算子 -slug: Web/JavaScript/Reference/Operators/Arithmetic_Operators -translation_of: Web/JavaScript/Reference/Operators -translation_of_original: Web/JavaScript/Reference/Operators/Arithmetic_Operators ---- -<div>{{jsSidebar("Operators")}}</div> - -<p><strong>算術運算子接受數值(資料型態為字串或變數都可)作為其運算子並回傳單個數值。 標準算術運算符號有加號(+),減號( - ),乘(*)和除(/)。</strong></p> - -<div>{{EmbedInteractiveExample("pages/js/expressions-arithmetic.html")}}</div> - - - -<h2 id="Addition_()"><a id="加法" name="加法">Addition (+)</a></h2> - -<p>加法運算子生成數字(運算元)的總和或字串串接。</p> - -<h3 id="Syntax">Syntax</h3> - -<pre class="syntaxbox"><strong>Operator:</strong> x + y -</pre> - -<h3 id="Examples">Examples</h3> - -<pre class="brush: js">// Number + Number -> addition -1 + 2 // 3 - -// Boolean + Number -> addition -true + 1 // 2 - -// Boolean + Boolean -> addition -false + false // 0 - -// Number + String -> concatenation -5 + 'foo' // "5foo" - -// String + Boolean -> concatenation -'foo' + false // "foofalse" - -// String + String -> concatenation -'foo' + 'bar' // "foobar" -</pre> - -<h2 id="Subtraction_(-)"><a id="減法" name="減法">Subtraction (-)</a></h2> - -<p>減法運算子能算出兩個運算元間的差異。</p> - -<h3 id="Syntax_2">Syntax</h3> - -<pre class="syntaxbox"><strong>Operator:</strong> x - y -</pre> - -<h3 id="Examples_2">Examples</h3> - -<pre class="brush: js">5 - 3 // 2 -3 - 5 // -2 -'foo' - 3 // NaN</pre> - -<h2 id="Division_()"><a id="除法" name="除法">Division (/)</a></h2> - -<p>The division operator produces the quotient of its operands where the left operand is the dividend and the right operand is the divisor.</p> - -<h3 id="Syntax_3">Syntax</h3> - -<pre class="syntaxbox"><strong>Operator:</strong> x / y -</pre> - -<h3 id="Examples_3">Examples</h3> - -<pre class="brush: js">1 / 2 // returns 0.5 in JavaScript -1 / 2 // returns 0 in Java -// (neither number is explicitly a floating point number) - -1.0 / 2.0 // returns 0.5 in both JavaScript and Java - -2.0 / 0 // returns Infinity in JavaScript -2.0 / 0.0 // returns Infinity too -2.0 / -0.0 // returns -Infinity in JavaScript</pre> - -<h2 id="Multiplication_(*)"><a id="乘法" name="乘法">Multiplication (*)</a></h2> - -<p>The multiplication operator produces the product of the operands.</p> - -<h3 id="Syntax_4">Syntax</h3> - -<pre class="syntaxbox"><strong>Operator:</strong> x * y -</pre> - -<h3 id="Examples_4">Examples</h3> - -<pre class="brush: js">2 * 2 // 4 --2 * 2 // -4 -Infinity * 0 // NaN -Infinity * Infinity // Infinity -'foo' * 2 // NaN -</pre> - -<h2 id="Remainder_()"><a id="餘數運算" name="餘數運算">Remainder (%)</a></h2> - -<p>The remainder operator returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend.</p> - -<h3 id="Syntax_5">Syntax</h3> - -<pre class="syntaxbox"><strong>Operator:</strong> var1 % var2 -</pre> - -<h3 id="Examples_5">Examples</h3> - -<pre class="brush: js">12 % 5 // 2 --1 % 2 // -1 -1 % -2 // 1 -NaN % 2 // NaN -1 % 2 // 1 -2 % 3 // 2 --4 % 2 // -0 -5.5 % 2 // 1.5 -</pre> - -<h2 id="Exponentiation_(**)"><a id="指數運算" name="指數運算">Exponentiation (**)</a></h2> - -<p>The exponentiation operator returns the result of raising first operand to the power second operand. that is, <code>var1</code><sup><code>var2</code></sup>, in the preceding statement, where <code>var1</code> and <code>var2</code> are variables. Exponentiation operator is right associative. <code>a ** b ** c</code> is equal to <code>a ** (b ** c)</code>.</p> - -<h3 id="Syntax_6">Syntax</h3> - -<pre class="syntaxbox"><strong>Operator:</strong> var1 ** var2 -</pre> - -<h3 id="Notes">Notes</h3> - -<p>In most languages like PHP and Python and others that have an exponentiation operator (**), the exponentiation operator is defined to have a higher precedence than unary operators such as unary + and unary -, but there are a few exceptions. For example, in Bash the ** operator is defined to have a lower precedence than unary operators. In JavaScript, it is impossible to write an ambiguous exponentiation expression, i.e. you cannot put a unary operator (<code>+/-/~/!/delete/void/typeof</code>) immediately before the base number.</p> - -<pre class="brush: js">-2 ** 2; -// 4 in Bash, -4 in other languages. -// This is invalid in JavaScript, as the operation is ambiguous. - - --(2 ** 2); -// -4 in JavaScript and the author's intention is unambiguous. -</pre> - -<h3 id="Examples_6">Examples</h3> - -<pre class="brush: js">2 ** 3 // 8 -3 ** 2 // 9 -3 ** 2.5 // 15.588457268119896 -10 ** -1 // 0.1 -NaN ** 2 // NaN - -2 ** 3 ** 2 // 512 -2 ** (3 ** 2) // 512 -(2 ** 3) ** 2 // 64 -</pre> - -<p>To invert the sign of the result of an exponentiation expression:</p> - -<pre class="brush: js">-(2 ** 2) // -4 -</pre> - -<p>To force the base of an exponentiation expression to be a negative number:</p> - -<pre class="brush: js">(-2) ** 2 // 4 -</pre> - -<div class="note"> -<p><strong>Note:</strong> JavaScript also has <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_XOR">a bitwise operator ^ (logical XOR)</a>. <code>**</code> and <code>^</code> are different (for example : <code>2 ** 3 === 8</code> when <code>2 ^ 3 === 1</code>.)</p> -</div> - -<h2 id="Increment_()"><a id="遞增運算" name="遞增運算">Increment (++)</a></h2> - -<p>The increment operator increments (adds one to) its operand and returns a value.</p> - -<ul> - <li>If used postfix, with operator after operand (for example, x++), then it returns the value before incrementing.</li> - <li>If used prefix with operator before operand (for example, ++x), then it returns the value after incrementing.</li> -</ul> - -<h3 id="Syntax_7">Syntax</h3> - -<pre class="syntaxbox"><strong>Operator:</strong> x++ or ++x -</pre> - -<h3 id="Examples_7">Examples</h3> - -<pre class="brush: js">// Postfix -var x = 3; -y = x++; // y = 3, x = 4 - -// Prefix -var a = 2; -b = ++a; // a = 3, b = 3 -</pre> - -<h2 id="Decrement_(--)"><a id="遞減運算" name="遞減運算">Decrement (--)</a></h2> - -<p>The decrement operator decrements (subtracts one from) its operand and returns a value.</p> - -<ul> - <li>If used postfix (for example, x--), then it returns the value before decrementing.</li> - <li>If used prefix (for example, --x), then it returns the value after decrementing.</li> -</ul> - -<h3 id="Syntax_8">Syntax</h3> - -<pre class="syntaxbox"><strong>Operator:</strong> x-- or --x -</pre> - -<h3 id="Examples_8">Examples</h3> - -<pre class="brush: js">// Postfix -var x = 3; -y = x--; // y = 3, x = 2 - -// Prefix -var a = 2; -b = --a; // a = 1, b = 1 -</pre> - -<h2 id="Unary_negation_(-)"><a name="Unary_negation">Unary negation (-)</a></h2> - -<p>The unary negation operator precedes its operand and negates it.</p> - -<h3 id="Syntax_9">Syntax</h3> - -<pre class="syntaxbox"><strong>Operator:</strong> -x -</pre> - -<h3 id="Examples_9">Examples</h3> - -<pre class="brush: js">var x = 3; -y = -x; // y = -3, x = 3 - -// Unary negation operator can convert non-numbers into a number -var x = "4"; -y = -x; // y = -4 -</pre> - -<h2 id="Unary_plus_()"><a name="Unary_plus">Unary plus</a> (+)</h2> - -<p>The unary plus operator precedes its operand and evaluates to its operand but attempts to convert it into a number, if it isn't already. Although unary negation (-) 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 ("0x"-prefixed) formats are supported. Negative numbers are supported (though not for hex). If it cannot parse a particular value, it will evaluate to {{jsxref("NaN")}}.</p> - -<h3 id="Syntax_10">Syntax</h3> - -<pre class="syntaxbox"><strong>Operator:</strong> +x -</pre> - -<h3 id="Examples_10">Examples</h3> - -<pre class="brush: js">+3 // 3 -+'3' // 3 -+true // 1 -+false // 0 -+null // 0 -+function(val){ return val } // NaN -</pre> - -<h2 id="Specifications">Specifications</h2> - -<table class="standard-table"> - <tbody> - <tr> - <th scope="col">Specification</th> - <th scope="col">Status</th> - <th scope="col">Comment</th> - </tr> - <tr> - <td>{{SpecName('ES1')}}</td> - <td>{{Spec2('ES1')}}</td> - <td>Initial definition.</td> - </tr> - <tr> - <td>{{SpecName('ES5.1', '#sec-11.3')}}</td> - <td>{{Spec2('ES5.1')}}</td> - <td>Defined in several sections of the specification: <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.6">Additive operators</a>, <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.5">Multiplicative operators</a>, <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.3">Postfix expressions</a>, <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.4">Unary operators</a>.</td> - </tr> - <tr> - <td>{{SpecName('ES2015', '#sec-postfix-expressions')}}</td> - <td>{{Spec2('ES2015')}}</td> - <td>Defined in several sections of the specification: <a href="http://www.ecma-international.org/ecma-262/6.0/#sec-additive-operators">Additive operators</a>, <a href="http://www.ecma-international.org/ecma-262/6.0/#sec-multiplicative-operators">Multiplicative operators</a>, <a href="http://www.ecma-international.org/ecma-262/6.0/#sec-postfix-expressions">Postfix expressions</a>, <a href="http://www.ecma-international.org/ecma-262/6.0/#sec-unary-operators">Unary operators</a>.</td> - </tr> - <tr> - <td>{{SpecName('ES2016', '#sec-postfix-expressions')}}</td> - <td>{{Spec2('ES2016')}}</td> - <td>Added <a href="https://github.com/rwaldron/exponentiation-operator">Exponentiation operator</a>.</td> - </tr> - <tr> - <td>{{SpecName('ES2017', '#sec-postfix-expressions')}}</td> - <td>{{Spec2('ES2017')}}</td> - <td> </td> - </tr> - <tr> - <td>{{SpecName('ESDraft', '#sec-additive-operators')}}</td> - <td>{{Spec2('ESDraft')}}</td> - <td> </td> - </tr> - </tbody> -</table> - -<h2 id="Browser_compatibility">Browser compatibility</h2> - - - -<p>{{Compat("javascript.operators.arithmetic")}}</p> - -<h2 id="See_also">See also</h2> - -<ul> - <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators">Assignment operators</a></li> -</ul> |