Arithmetic operators

Arithmetic operators are used to perform simple mathematical calculations. Arithmetic operators can be divided into two categories:

  1. binary – those operators that work on elements in an expression.
  2. unary – those operators that work on only one element or variable.

Table 1 lists binary arithmetic operators used in JavaScirpt.

Table 1 Binary arithmetic operators
Operator Name Description Example
+ Addition operator Adds two values together. In JavaScript, this operators is also used to combine two elements. var webPages = 100;
var images = 25;
var TotalFiles = webPages + images;
var message = "Hello " + "Sam";
- Subtraction operator Subtracts one value from another. var totalAmount = 100;
var discount = 3.50;
var totalAmountDue = totalAmount - discount;
/ Division operator Divides one value by another var totalCost = 300;
var numOfItems = 7;
var averagePricePerItem = totalCost / numOfItems
* Multiplication operator Multiples two values together var pricePerItem = 5.50;
var quantityPurchased = 40;
var totalPurchaseCost = pricePerItem * quantityPurchased;
% Modulus operator Determines the remainder after dividing one value by another var result = 5 % 2;

Table 2 summarizes unary operators. By using unary operators, you could save some typing; consider the following as an example.

var x = 5;
x = x + 1;

On the first line, we declare a variable called x and assign the number 5. Can you guess what is x after line 2 has been executed? If you think, it is 6; you are correct. Let's explain how we got 6. On line 1, we set x to 5, and on line 2, we add 5 more to x. In other words, line 2 is saying: x is equal to 5 + 1, which is 6. In this example, note we are using the addition operator (+).

Table 2 unary arithmetic operators
Operator Operator Name Description Example
++ Increment operator Increases a value by 1 a = 10;
b = a++;
-- Decrement operator Decreases a value by 1 a = 10;
b = a--;
- Negation operator Changes the sign of a value a = -10;
b = -a;

In the following example, we will use the increment operator, used to increase the value of a variable by 1.

var x = 5;
x = x++;

So in this example, what is the value of x after line 2 is executed? It is 6 again because the increment operator on line 2 adds 1 to x, which was set to five on line 1. The increment operator comes handy when you have to increase the value of a variable by 1, for instance, when working with loops (executing the same code more than once).

The decrement operator does just the opposite of an increment operator. The decrement operator reduces the value of a variable by 1. The decrement operator also is useful in loops.

The negation operator simply changes the sign of value, assigned to a variable. For instance, in the following

var x = 5;
var y = -x;

the value of y is -5. Why? On line 1, we set x to 5. On line 2, we assign -5 to variable y. Note the value of x will still be 5 after line 2 has executed. Remember a value of a variable changes only when you use the assignment operator. The assignment operator on line 2 changes only the value of y.