Arithmetic Operators

Arithmetic Operators are those which are used to calculate arithmetic operaton like addition, subtraction, multiplication etc. They are learnt in school. They always works on number.

In most programming language the following arithmetic operators are always available -

Addition Operator

This operator takes two operand and sums up and then returns the result.

5 + 7     // 12
-5 + 7    // 2
-5 + -7   // -12
a + b
x + y 
let a = 30;
let b = 10;
let c = a + b;
console.log(c);                 // 40

Subtraction Operator

This operator takes two operand and subtracts the second number from first number and then returns it.

9 - 2          // 7
-7 - 8         // -15
19 - 23        // -4
15 - 10        // 5
x - y
a - a          // 0
let a = 30;
let b = 10;
let c = a - b;
console.log(c);                 // 20

Multiplication Operator

This operator takes two operand and multiply two numbers and then returns it.

2 * 7              // 14
6 * -6             // -36
-6 * -6            // 36
5 * 0              // 0
a * b              
let a = 30;
let b = 10;
let c = a * b;
console.log(c);                 // 300

Division Operator

This operator takes two operand and multiply two numbers and then returns it. But the result varies in differnt language. In some language if you divide integer number with integer number, the result will be integer too. If the result yeilds to floating number, then the fraction part gets truncated. While in other language, if the result yields to float value, then the float value is returned.

For example in C programming language -

5 / 2                // 2

And in JavaScript -

5 / 2                // 2.5
x / y
let a = 30;
let b = 10;
let c = a / b;
console.log(c);                 // 3

Modular Division Operator

Modular division operator is also called Reminder Operator as it returns the reminder of division. While the Division operator returns the result, Reminder operator returns the reminder.

5 % 2           // 1
4 % 2           // 0
53 % 7          // 4
a % b
let a = 30;
let b = 10;
let c = a % b;
console.log(c);                 // 0

Negation Operator

The Negation operator - same as Subtraction operator. But difference is how many operand the operator is applied to. If you apply - operator to two operand it becomes subtraction operator and if you apply to one operand it becomes Negation operator. Negation Operation is a Unary Operator as it takes only one operand and Subtraction Operator is called Binary Operator as it takes two operand.

The following example turns positive number to negative number -

let a = 30;
a = -a;
console.log(a);          // -30

The following example turns negative number to positive number -

let a = -30;
a = -a;
console.log(a);          // 30

You can also apply to number directly -

-30               // -30
-(-30)            // 30

Here parenthesis is required. If you don't use parenthesis, then it becomes --30 which means Prefix Decrement Operator which you will learn later.

Increment Operator

Increment Operator increments variable by one and then returns it.

let a = 30, b = -20;
a++;
console.log(a);         // 31
b++;
let c = a++;            
console.log(b);         // -19
console.log(c);         // 31
console.log(a);         // 32

Unlinke any other arithmetic operator it cannot be used directly on number. So the followings are invalid -

console.log(30++);             // Error
console.log(-30++);            // Also Error

The following two are equivalent -

let a = 30;
a = a + 1;



let a = 30;
a++;

Decrement Operator

Decrement Operator decrements the variable by one and then returns it.

let a = 30, b = -10;
a--;
console.log(a);           // 29
b--;
let c = a--;              
console.log(b);           // -11
console.log(c);           // 29
console.log(a);           // 28

Just like Increment operator you cannot use this operator directly on numbers. It will result an error.

The followings two are equivalent -

let a = 30;
a--;


let a = 30;
a = a - 1;

Postfix and Prefix Notation

The Increment and Decrement operator comes in two variation. One is Postfix Notation and another is Prefix Notation. In Postfix notation the operator is written after the operand and in Prefix notation the operator is written before the operand.

--a;      // Prefix Form
--b;      // Prefix Form


a++;      // Postfix Form
b++;      // Postfix Form

In the above example, you won't see any difference as the there are no other expression. The whole statement is about incrementing and decrementing values. But if you use them with other expression the Postfix and Prefix Form acts differently.

Prefix Increment first increment the variable and then the returned value is used in the expression. While Postfix form of the Increment operator increment the operator after the value is used within the expression. The same logic goes for Decrement operator too.

Consider the following example -

let a = 30, b = 20;
let c = a + b++;
console.log(c);            // 50


let x = 30, y = 20;
let z = x + ++y;
console.log(z);            // 51

In the first example, we have used Postfix notation. It means the value is used first so a+b is performed first which is 30+20 becomes 50 and then the value is stored in the variable c, and after the whole operation, the b is incremented by one.

In the second example, we have used Prefix notation. It means the value is incremented first and then the value will be used in the expression. So ++y is performed first which result 21 and then the addtion of x+y which is 30+21 is performed which results 51

The same logic is applied to decrement operator.