JavaScript provides a variety of operators for performing mathematical operations. These operators can be used to perform arithmetic, assign values, compare values, and more. Here are some of the most commonly used math operators in JavaScript:

Arithmetic Operators

  • Addition (+): Adds two numbers.
  let sum = 10 + 5;
console.log(sum); // 15
  
  • Subtraction (-): Subtracts one number from another.
  let difference = 10 - 5;
console.log(difference); // 5
  
  • Multiplication (*): Multiplies two numbers.
  let product = 10 * 5;
console.log(product); // 50
  
  • Division (/): Divides one number by another.
  let quotient = 10 / 5;
console.log(quotient); // 2
  
  • Modulus (%): Returns the remainder of a division operation.
  let remainder = 10 % 3;
console.log(remainder); // 1
  
  • Exponentiation ()**: Raises a number to the power of another number.
  let power = 2 ** 3;
console.log(power); // 8
Increment and Decrement Operators
  
  • Increment (++): Increases a number by one.
  let a = 5;
a++;
console.log(a); // 6
  
  • Decrement (–): Decreases a number by one.
  let b = 5;
b--;
console.log(b); // 4
  

Assignment Operators

  • Assignment (=): Assigns a value to a variable.
  let x = 10;
console.log(x); // 10
  
  • Addition Assignment (+=): Adds a value to a variable and assigns the result to the variable.
  let x = 10;
x += 5;
console.log(x); // 15
  
  • Subtraction Assignment (-=): Subtracts a value from a variable and assigns the result to the variable.
  let x = 10;
x -= 5;
console.log(x); // 5
  
  • Multiplication Assignment (*=): Multiplies a variable by a value and assigns the result to the variable.
  let x = 10;
x *= 5;
console.log(x); // 50
  
  • Division Assignment (/=): Divides a variable by a value and assigns the result to the variable.
  let x = 10;
x /= 5;
console.log(x); // 2
  
  • Modulus Assignment (%=): Takes the modulus of a variable and a value and assigns the result to the variable.
  let x = 10;
x %= 3;
console.log(x); // 1
  
  • Exponentiation Assignment (=)**: Raises a variable to the power of a value and assigns the result to the variable.
  let x = 2;
x **= 3;
console.log(x); // 8
  

Comparison Operators

  • Equal (==): Checks if two values are equal.
  console.log(5 == "5"); // true
  
  • Strict Equal (===): Checks if two values are equal and of the same type.
  console.log(5 === "5"); // false
  
  • Not Equal (!=): Checks if two values are not equal.
  console.log(5 != "5"); // false
  
  • Strict Not Equal (!==): Checks if two values are not equal or not of the same type.
  console.log(5 !== "5"); // true
  
  • Greater Than (>): Checks if the left value is greater than the right value.
  console.log(10 > 5); // true
  
  • Greater Than or Equal (>=): Checks if the left value is greater than or equal to the right value.
  console.log(10 >= 5); // true
  
  • Less Than (<): Checks if the left value is less than the right value.
  console.log(5 < 10); // true
  
  • Less Than or Equal (<=): Checks if the left value is less than or equal to the right value.
  console.log(5 <= 10); // true
  

JavaScript provides a wide range of math operators for performing arithmetic, assignment, and comparison operations. Understanding these operators is essential for writing effective and efficient JavaScript code. Here are a few more advanced examples to demonstrate the use of these operators:

Advanced Examples

  • Calculating the Area of a Circle:
  let radius = 5;
let area = Math.PI * radius ** 2;
console.log(area); // 78.53981633974483
  
  • Swapping Values Using Destructuring:
  let a = 3;
let b = 5;
[a, b] = [b, a];
console.log(a, b); // 5 3
  
  • Using Modulus to Determine Even or Odd:
  let num = 10;
if (num % 2 === 0) {
    console.log(`${num} is even.`);
} else {
    console.log(`${num} is odd.`);
}
  

By mastering these operators, you can perform a wide range of mathematical and logical operations in your JvaScript programs.