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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
---
title: Arithmetic operators
slug: conflicting/Web/JavaScript/Reference/Operators
translation_of: Web/JavaScript/Reference/Operators
translation_of_original: Web/JavaScript/Reference/Operators/Arithmetic_Operators
original_slug: Web/JavaScript/Reference/Operators/Arithmetic_Operators
---
<div>{{jsSidebar("Operators")}}</div>
<p><strong>Аритметичните оператори </strong>приемат числови стойности като техен операнд и връща единична числова стойност. Стандартните аритметични оператори са събиране (<strong>+</strong>), изваждане (<strong>-</strong>), умножение (<strong>*</strong>), делене(<strong>/</strong>)</p>
<div>{{EmbedInteractiveExample("pages/js/expressions-arithmetic.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="Addition" name="Addition">Събиране (+)</h2>
<p>Операторът за събиране произвежда сумата от числови операнди или конкатенация на стрингове.</p>
<h3 id="Синтаксис">Синтаксис</h3>
<pre class="syntaxbox"><strong>Operator:</strong> <var>x</var> + <var>y</var>
</pre>
<h3 id="Примери">Примери</h3>
<pre class="brush: js">// Number + Number -> събиране
1 + 2 // 3
// Boolean + Number -> събиране
true + 1 // 2
// Boolean + Boolean -> събиране
false + false // 0
// Number + String -> конкатенация
5 + 'foo' // "5foo"
// String + Boolean -> конкатенация
'foo' + false // "foofalse"
// String + String -> конкатенация
'foo' + 'bar' // "foobar"
</pre>
<h2 id="Subtraction" name="Subtraction">Subtraction (-)</h2>
<p>The subtraction operator subtracts the two operands, producing their difference.</p>
<h3 id="Syntax">Syntax</h3>
<pre class="syntaxbox"><strong>Operator:</strong> <var>x</var> - <var>y</var>
</pre>
<h3 id="Examples">Examples</h3>
<pre class="brush: js">5 - 3 // 2
3 - 5 // -2
'foo' - 3 // NaN</pre>
<h2 id="Division" name="Division">Division (/)</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_2">Syntax</h3>
<pre class="syntaxbox"><strong>Operator:</strong> <var>x</var> / <var>y</var>
</pre>
<h3 id="Examples_2">Examples</h3>
<pre class="brush: js">1 / 2 // returns 0.5 in JavaScript (In Java, returns 0; both are integers)
// (neither 1 nor 2 is explicitly a floating point number)
Math.floor(3 / 2) // returns 1
1.0 / 2.0 // returns 0.5 in both JavaScript and Java
2.0 / 0 // returns Infinity
2.0 / 0.0 // ditto, because 0.0 === 0
2.0 / -0.0 // returns -Infinity</pre>
<h2 id="Multiplication" name="Multiplication">Multiplication (*)</h2>
<p>The multiplication operator produces the product of the operands.</p>
<h3 id="Syntax_3">Syntax</h3>
<pre class="syntaxbox"><strong>Operator:</strong> <var>x</var> * <var>y</var>
</pre>
<h3 id="Examples_3">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" name="Remainder">Remainder (%)</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_4">Syntax</h3>
<pre class="syntaxbox"><strong>Operator:</strong> <var>var1</var> % <var>var2</var>
</pre>
<h3 id="Examples_4">Examples</h3>
<pre class="brush: js"> 12 % 5 // 2
-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" name="Exponentiation">Exponentiation (**)</h2>
<p>The exponentiation operator returns the result of raising the first operand to the power of the second operand. That is, <code><var>var1</var></code><sup><code><var>var2</var></code></sup>, in the preceding statement, where <code><var>var1</var></code> and <code><var>var2</var></code> are variables. The exponentiation operator is right-associative. <code><var>a</var> ** <var>b</var> ** <var>c</var></code> is equal to <code><var>a</var> ** (<var>b</var> ** <var>c</var>)</code>.</p>
<h3 id="Syntax_5">Syntax</h3>
<pre class="syntaxbox"><strong>Operator:</strong> <var>var1</var> ** <var>var2</var>
</pre>
<h3 id="Notes">Notes</h3>
<p>In most languages, such as PHP, Python, and others that have an exponentiation operator (<code>**</code>), the exponentiation operator is defined to have a higher precedence than unary operators, such as unary <code>+</code> and unary <code>-</code>, but there are a few exceptions. For example, in Bash, the <code>**</code> operator is defined to have a lower precedence than unary operators.</p>
<p>In JavaScript, it is impossible to write an ambiguous exponentiation expression; that is, 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_5">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" name="Increment">Increment (++)</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, <code><var>x</var>++</code>), then it increments and returns the value before incrementing.</li>
<li>If used prefix, with operator before operand (for example, <code>++<var>x</var></code>), then it increments and returns the value after incrementing.</li>
</ul>
<h3 id="Syntax_6">Syntax</h3>
<pre class="syntaxbox"><strong>Operator:</strong> <var>x</var>++ or ++<var>x</var>
</pre>
<h3 id="Examples_6">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" name="Decrement">Decrement (--)</h2>
<p>The decrement operator decrements (subtracts one from) its operand and returns a value.</p>
<ul>
<li>If used postfix, with operator after operand (for example, <code><var>x</var>--</code>), then it decrements and returns the value before decrementing.</li>
<li>If used prefix, with operator before operand (for example, <code>--<var>x</var></code>), then it decrements and returns the value after decrementing.</li>
</ul>
<h3 id="Syntax_7">Syntax</h3>
<pre class="syntaxbox"><strong>Operator:</strong> <var>x</var>-- or --<var>x</var>
</pre>
<h3 id="Examples_7">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" name="Unary_negation">Unary negation (-)</h2>
<p>The unary negation operator precedes its operand and negates it.</p>
<h3 id="Syntax_8">Syntax</h3>
<pre class="syntaxbox"><strong>Operator:</strong> -<var>x</var>
</pre>
<h3 id="Examples_8">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" name="Unary_plus">Unary plus (+)</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 (<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). If it cannot parse a particular value, it will evaluate to {{jsxref("NaN")}}.</p>
<h3 id="Syntax_9">Syntax</h3>
<pre class="syntaxbox"><strong>Operator:</strong> +<var>x</var>
</pre>
<h3 id="Examples_9">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">
<thead>
<tr>
<th scope="col">Specification</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('ESDraft', '#sec-additive-operators', 'Additive operators')}}</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-postfix-expressions', 'Postfix expressions')}}</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-11.5', 'Multiplicative operators')}}</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-11.4', 'Unary operator')}}</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>
|