In JavaScript, arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value. There are four standard arithmetic operators, addition (+), subtraction (-), multiplication (*), and division (/).
These operators work as they do in other programming languages except the division (/) operator which returns a floating-point division in JavaScript, not a truncated division as it does in languages such as C or Java.
For example:
1/2 returns 0.5 in JavaScript.
1/2 returns 0 in Java.
In addition, JavaScript provides modules(%), increment(++), decrement(–) and unary negation(-) operators.
Operator Name Purpose Example
+ Addition Adds two numbers together. 6 + 9
– Subtraction Subtracts the right number from the left. 20 – 15
* Multiplication Multiplies two numbers together. 3 * 7
/ Division Divides the left number by the right. 10 / 5
% Remainder (sometimes called modulo)
Returns the remainder left over after you’ve divided the left number into a
number of integer portions equal to the right number.
8 % 3 (returns 2, as three goes into 8 twice, leaving 2 left over).
** Exponent Raises a base number to the exponent power, that is, the base number
multiplied by itself, exponent times. 5 ** 2 (returns 25, which is the same as 5 * 5).
Hi Sheree, I really found your post easy to follow and understand. The analysis of arithmetic operators in JavaScript is great and the examples shown really help to understand how they function in programs. Just can’t forget that JavaScript operates with PEMDAS of course.