ECMAScript Multiplicative Operators

The multiplicative operators in ECMAScript are similar in operation to those in similar operators in languages such as Java, C, and Perl.

It should be noted that the multiplicative operator also has some automatic conversion functions.

Multiplication operator

The multiplication operator is represented by an asterisk (*), used for multiplying two numbers.

The multiplication syntax in ECMAScript is the same as in C language:

var iResult = 12 * 34

However, when dealing with special values, there are some special behaviors in multiplication in ECMAScript:

  • If the result is too large or too small, the generated result is Infinity or -Infinity.
  • If any operand is NaN, the result is NaN.
  • Infinity multiplied by 0 results in NaN.
  • Infinity multiplied by any number other than 0 results in Infinity or -Infinity.
  • Infinity multiplied by Infinity results in Infinity.

Note:If the operands are numbers, then perform the usual multiplication operation, that is, two positive numbers or two negative numbers are positive, and the signs of the two operands are different, the result is negative.

Division Operator

The division operator is represented by the forward slash (/), using the second operand to divide the first operand:

var iResult = 88 / 11;

Similar to the multiplication operator, the division operator also has some special behaviors when dealing with special values:

  • If the result is too large or too small, the generated result is Infinity or -Infinity.
  • If any operand is NaN, the result is NaN.
  • Infinity divided by Infinity results in NaN.
  • Infinity divided by any number results in Infinity.
  • 0 divided by any non-infinite number results in NaN.
  • Infinity divided by any number other than 0 results in Infinity or -Infinity.

Note:If the operands are numbers, then perform the usual division operation, that is, two positive numbers or two negative numbers are positive, and the signs of the two operands are different, the result is negative.

Modulus Operator

The division (remainder) operator is represented by the percent sign (%), and the usage is as follows:

var iResult = 26%5; // equals 1

Similar to other multiplicative operators, the modulus operator also has special behaviors for special values:

  • If the dividend is Infinity or the divisor is 0, the result is NaN.
  • Infinity divided by Infinity results in NaN.
  • If the divisor is an infinite number, the result is the dividend.
  • If the dividend is 0, the result is 0.

Note:If the operands are numbers, then perform the usual arithmetic division operation, returning the remainder obtained from the division operation.