Arithmetic Operations

Question: What arithmetic operations are supported in JavaScript?

Answer: JavaScript supports the following arithmetic operations (which you can group in brackets to form more complex expressions):

Unary operations have one argument (in the following examples, the argument is a):

-a   // change the sign of a
~a   // bitwise NOT a
++a  // add 1 to a (before using a)
a++  // add 1 to a (after using a)
--a  // subtract 1 from a (before using a)
a--  // subtract 1 from a (after using a)

Binary operations operations have two arguments (in the following examples, the arguments are a and b):

a * b    // multiply a by b
a / b    // divide a by b
a % b    // find the remainder of division of a by b
a + b    // add a and b
a - b    // subtract b from a
a & b    // bitwise a AND b
a | b    // bitwise a OR b
a ^ b    // bitwise a XOR b

Shifts are the following operations:

a << b   // shift a by b bits to the left
         // (padding with zeros)
a >> b   // shift a by b bits to the right
         // (copying the sign bit)
a >>> b  // shift a by b bits to the right 
         // (padding with zeros)

Copyright © 1999-2008, JavaScripter.net.