Home Courses Arithmetic Operators Explained

Arithmetic Operators Explained

What are Arithmetic Operators?

Arithmetic operators are used to perform mathematical operations.

  1. Examples:
  2. Addition
  3. Subtraction
  4. Multiplication
  5. Division
  6. Increment / Decrement


List of Arithmetic Operators

Operator
+Addition
-Subtraction
*Multiplication
/Division
%Modulus (remainder)
**Exponentiation (power)
++Increment
--Decrement


Basic Example

let a = 10;
let b = 5;

console.log(a + b); // 15
console.log(a - b); // 5
console.log(a * b); // 50
console.log(a / b); // 2
console.log(a % b); // 0
console.log(a ** b); // power


Modulus Operator (%)

  1. Gives remainder
10 % 5 // 0


Exponentiation (**)

  1. Used for power
10 ** 2 // 100


Increment and Decrement

Post Increment (a++)

  1. First use value, then increase
let a = 10;
console.log(a++); // 10
console.log(a); // 11


Pre Increment (++a)

  1. First increase, then use value
let a = 10;
console.log(++a); // 11


Same for Decrement (--)

let a = 10;
console.log(a--); // 10
console.log(a); // 9


Important Difference (Interview)

Type
a++Print first, then increment
++aIncrement first, then print


Tricky Interview Questions

1. String + Number

console.log("5" + 1); // "51"
  1. String concatenation happens


2. String - Number

console.log("5" - 1); // 4
  1. Converted to number


3. String * Number

console.log("5" * 2); // 10


4. Invalid String

console.log("abc" * 2); // NaN
  1. Not a Number


5. Boolean Addition

console.log(true + true); // 2
  1. true = 1, false = 0


6. Double Negative

console.log(5 - -2); // 7


7. Operator Precedence

console.log(5 + 2 * 10); // 25
  1. First multiplication → 2 * 10 = 20
  2. Then addition → 5 + 20 = 25


Key Points to Remember

  1. Only + does string concatenation
  2. Other operators convert values to numbers
  3. NaN comes when conversion is not possible
  4. Pre and Post increment behave differently
  5. Operator precedence matters


Function Example from Code

function operations() {
let a = 10;
let b = 5;

console.log(a + b);
}



Share this lesson: