Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Operatori Aritmetici

Questo articolo richiede una revisione stilistica. Scopri come puoi essere d’aiuto.`

Questa traduzione è incompleta. Collabora alla traduzione di questo articolo dall’originale in lingua inglese.

Gli operatori aritmetici lavorano su operandi numerici (sia letterali che variabili) e ritornano un singolo valore numerico. Gli operatori aritmetici standard sono l'addizione (+), la sottrazione (-), la moltiplicazione (*) e la divisione (/).

 

Addizione (+)

 

L'operazione di addizione produce la somma di operandi numerici o la concatenzione di stringhe.

Sintassi

Operator: x + y

Esempi

// Numero + Numero -> addizione
1 + 2 // 3

// Booleano + Numero -> addizione
true + 1 // 2

// Booleano + Booleano -> additione
false + false // 0

// Numero + Stringa -> concatenazione
5 + "foo" // "5foo"

// Stringa + Booleano -> concatenazione
"foo" + false // "foofalse"

// Stringa + Stringa -> concatenazione
"foo" + "bar" // "foobar"

Sottrazione (-)

L'operatore di sottrazione fa la sottrazione dei due operandi e produce la loro differenza.

Sintassi

Operator: x - y

Esempi

5 - 3 // 2
3 - 5 // -2
"foo" - 3 // NaN

Divisione (/)

L'operatore di divisione produce il quoziente dei suoi operandi dove l'operando di sinistra è il dividendo e l'operando di destra è il divisore.

Sintassi

Operator: x / y

Esempi

1 / 2      // ritorna 0.5 in JavaScript
1 / 2      // ritorna 0 in Java 
// (nessuno degli operandi è un numero in virgola mobile esplicito)

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

Moltiplicazione (*)

The multiplication operator produces the product of the operands.

Syntax

Operator: x * y

Examples

2 * 2 // 4
-2 * 2 // -4
Infinity * 0 // NaN
Infinity * Infinity // Infinity
"foo" * 2 // NaN

Remainder (%)

The remainder operator returns the first operand modulo the second operand, that is, var1 modulo var2, in the preceding statement, where var1 and var2 are variables. The modulo function is the integer remainder of dividing var1 by var2There is a proposal to get an actual modulo operator in a future version of ECMAScript.

Syntax

Operator: var1 % var2

Examples

12 % 5 // 2
-1 % 2 // -1
NaN % 2 // NaN
1 % 2 // 1
2 % 3 // 2
-4 % 2 // -0

Exponentiation (**)

This is an experimental technology, part of the ECMAScript 2016 (ES7) proposal.
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.

The exponentiation operator returns the result of raising first operand to the power second operand. that is, var1var2, in the preceding statement, where var1 and var2 are variables. Exponentiation operator is right associative. a ** b ** c is equal to a ** (b ** c).

Syntax

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 other unary operators such as unary + and unary -, but there are a few exceptions. For example, in Bash or in the current ES7 exponentiation operator draft spec, the ** operator is defined to have a lower precedence than unary operators.

-2 ** 2 // equals 4 in ES7 or in Bash, equals -4 in other languages.

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

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 converts 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

Specifications

Specification Status Comment
ECMAScript 1st Edition. 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 7 Draft Exponentiation operator.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
Exponentiation operator ? Nightly build ? ? ?
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)
Exponentiation operator ? ? Nightly build ? ? ?

See also

Tag del documento e collaboratori

 Hanno collaborato alla realizzazione di questa pagina: alberto.decaro
 Ultima modifica di: alberto.decaro,