Skip to main content

Mathematical Operators

Introduction​

Mathematical operators allow us to perform calculations on numbers. These include basic arithmetic, working with remainders, exponents, and updating variables with assignment operators.

JavaScript uses the number type for both integers and floating-point numbers (decimals). Unlike some languages, there’s no distinction between int and float.

Operators are used to add, subtract, multiply, divide, and more.


Concepts​

Arithmetic​

  • + addition
  • - subtraction
  • * multiplication
  • / division
  • % remainder
  • ** exponentiation

Increment/Decrement​

  • ++ (prefix/postfix)
  • -- (prefix/postfix)

Assignment Operators​

  • = assignment
  • +=, -=, *=, /=, %= update a variable in place

Examples​

let a = 10, b = 3;
console.log(a + b); // 13
console.log(a % b); // 1
console.log(a ** b); // 1000

let x = 5;
console.log(x++); // 5
console.log(x); // 6
console.log(++x); // 7

let y = 10;
y *= 2;
console.log(y); // 20

Challenges​

1. Calculate area of rectangle​

  • Write a program to calculate the area of a rectangle with length = 8 and width = 5.
💡 Show Answer
let length = 8, width = 5;
console.log(length * width); // 40

2. Find the remainder of 23 ÷ 6​

💡 Show Answer
console.log(23 % 6); // 5

3. Compute 3⁴​

  • Use ** to calculate 3 to the power of 4.
💡 Show Answer
console.log(3 ** 4); // 81

4. Celsius to Fahrenheit​

  • Write a temperature converter that converts 25°C into Fahrenheit using the formula F = (C × 9/5) + 32.
💡 Show Answer
let c = 25;
let f = (c * 9/5) + 32;
console.log(f); // 77

5. Add prices and apply discount​

  • Simulate a shopping cart: add three item prices, then apply a 15% discount using *=.
💡 Show Answer
let total = 100 + 50 + 20;
let discounted = total * 0.85;
console.log(discounted); // 144.5

6. Use ++ and --​

  • Start with x = 10. Use increment and decrement operators to:
    • Increase it twice,
    • Decrease it once,
    • Print the final value.
💡 Show Answer
let x = 10;
x++;
x++;
x--;
console.log(x); // 11

7. Dozens of eggs in 50​

  • Use / and % to calculate how many full dozens (12s) fit into 50 eggs, and how many are left over.
💡 Show Answer
let eggs = 50;
console.log(Math.floor(eggs/12)); // 4
console.log(eggs % 12); // 2

8. Factorial calculator​

  • Create a factorial calculator using a for loop and multiplication.
💡 Show Answer
function factorial(n){
let result = 1;
for (let i=1; i<=n; i++) result *= i;
return result;
}
console.log(factorial(5)); // 120

9. Dice roller​

  • Build a program that rolls a six-sided dice (1–6) using Math.random().
💡 Show Answer
function rollDice(){
return Math.floor(Math.random()*6)+1;
}
console.log(rollDice());

10. Compound interest​

  • Write a function compoundInterest(principal, rate, years) that returns the total amount after compounding yearly.
  • (Hint: use **.)
💡 Show Answer
function compoundInterest(p,r,y){
return p * (1+r)**y;
}
console.log(compoundInterest(1000,0.05,10).toFixed(2));