این ترجمه ناقص است. لطفاً در ترجمه این مقاله از انگلیسی کمک کنید.
عملگر بعلاوه (+)
عملگر بعلاوه، در جاوااسکریپت دو مقدار را با یکدیگر جمع می کند. و حاصل را برمی گرداند.
نحوه ی نوشتن(سینتکس)
Operator: x + y
به مثال های زیر توجه کنید:
// 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"
عملگر تفریق (-)
عملگر تفریق، دو مقدار را میگیرد و آنها را از یکدیگر تفریق می کند و حاصل را برمی گرداند.
نحوه ی نوشتن(سینتکس)
Operator: x - y
به مثال های زیر توجه کنید:
5 - 3 // 2 3 - 5 // -2 'foo' - 3 // NaN
عملگر تقسیم (/)
عملگر تقسیم دو مقدار میگیرد و عدد سمت چپ را بر عدد سمت راست تقسیم می کند.
نحوه ی نوشتن(سینتکس)
Operator: x / y
Examples
1 / 2 // در جاوااسکریپت مقدار 0.5 را برمی گرداند 1 / 2 // در جاوا مقدار 0 را برمیگرداند // (اعداد صراحتاً نیازی نیست که عدد اعشاری باشند) 1.0 / 2.0 // در جاوا و جاوااسکریپت مقدار 0.5 را برمیگرداند 2.0 / 0 // در جاوااسکریپت بینهایت را بر می گرداند 2.0 / 0.0 // این یکی هم مقدار بینهایت را برمیگرداند 2.0 / -0.0 // در جاوااسکریپت مقدار منفی بینهایت را برمیگرداند
عملگر ضرب (*)
این عملگر حاصلضرب دو عدد را نمایش می دهد.
نحوه ی نوشتن(سینتکس)
Operator: x * y
Examples
2 * 2 // 4 -2 * 2 // -4 Infinity * 0 // NaN Infinity * Infinity // Infinity 'foo' * 2 // NaN
باقیمانده تقسیم (%)
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, not the divisor. It uses a built-in modulo function to produce the result, which is the integer remainder of dividing var1 by var2 — for example — var1 modulo var2. There is a proposal to get an actual modulo operator in a future version of ECMAScript, the difference being that the modulo operator result would take the sign of the divisor, not the dividend.
نحوه ی نوشتن(سینتکس)
Operator: var1 % var2
Examples
12 % 5 // 2 -1 % 2 // -1 NaN % 2 // NaN 1 % 2 // 1 2 % 3 // 2 -4 % 2 // -0 5.5 % 2 // 1.5
به توان رساندن (**)
این عملگر عدد اول را به توان عدد دوم می رساند. ظاهر کلی آن به این صورت است: Xy, که X و y مقادیر عددی هستند. تقدم انجام این عملگر از راست است. a ** b ** c برابر است با (a ** (b ** c.
نحوه ی نوشتن(سینتکس)
Operator: var1 ** var2
Notes
In most languages like PHP and Python and others that have an exponentiation operator (typically ^ or **), 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 (+/-/~/!/delete/void/typeof) immediately before the base number.
-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.
Examples
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
To invert the sign of the result of an exponentiation expression:
-(2 ** 2) // -4
To force the base of an exponentiation expression to be a negative number:
(-2) ** 2 // 4
Increment (++)
The increment operator increments (adds one to) its operand and returns a value.
- If used postfix, with operator after operand (for example, x++), then it returns the value before incrementing.
- If used prefix with operator before operand (for example, ++x), then it returns the value after incrementing.
Syntax
Operator: x++ or ++x
Examples
// Postfix var x = 3; y = x++; // y = 3, x = 4 // Prefix var a = 2; b = ++a; // a = 3, b = 3
Decrement (--)
The decrement operator decrements (subtracts one from) its operand and returns a value.
- If used postfix (for example, x--), then it returns the value before decrementing.
- If used prefix (for example, --x), then it returns the value after decrementing.
Syntax
Operator: x-- or --x
Examples
// Postfix var x = 3; y = x--; // y = 3, x = 2 // Prefix var a = 2; b = --a; // a = 1, b = 1
Unary negation (-)
The unary negation operator precedes its operand and negates it.
Syntax
Operator: -x
Examples
var x = 3; y = -x; // y = -3, x = 3
Unary plus (+)
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 true, false, and null. 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 NaN.
Syntax
Operator: +x
Examples
+3 // 3
+'3' // 3
+true // 1
+false // 0
+null // 0
+function(val){ return val } // NaN
Specifications
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. |
| ECMAScript 5.1 (ECMA-262) | Standard | Defined in several sections of the specification: Additive operators, Multiplicative operators, Postfix expressions, Unary operators. |
| ECMAScript 2015 (6th Edition, ECMA-262) | Standard | Defined in several sections of the specification: Additive operators, Multiplicative operators, Postfix expressions, Unary operators. |
| ECMAScript 2016 (ECMA-262) | Standard | Added Exponentiation operator. |
| ECMAScript Latest Draft (ECMA-262) | Draft |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| Exponentiation operator | 52.0 | 52.0 (52.0) | ? | ? | ? |
| Feature | Android | Android Webview | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| Exponentiation operator | No support | 51.0 | 52.0 (52.0) | ? | ? | ? | 52.0 |