Arithmetic Operators in JavaScript
Purpose
- Arithmetic operators perform basic mathematical calculations on numbers.
Common Arithmetic Operators:
- Addition (+): Adds two numbers together.
- let sum = 5 + 3; // sum will be 8
- Subtraction (-): Subtracts the second number from the first.
- let difference = 10 – 4; // difference will be 6.
- Multiplication (*): Multiplies two numbers.
- let product = 5 * 3; // product will be 15
- Division (/): Divides the first number by the second.
- let quotient = 10 / 2; // quotient will be 5
- Modulus (%): Returns the remainder after division.
- let remainder = 10 % 3; // remainder will be 1
- Exponentiation (**): Raises a number to the power of another.
- let result = 2 ** 3; // result will be 8 (2 raised to the power of 3)
Example:
let num1 = 10; let num2 = 5; let sum = num1 + num2; let difference = num1 - num2; let product = num1 * num2; let quotient = num1 / num2; let remainder = num1 % num2; let power = num1 ** 2; // 10 raised to the power of 2 console.log("Sum:", sum); console.log("Difference:", difference); console.log("Product:", product); console.log("Quotient:", quotient); console.log("Remainder:", remainder); console.log("Power:", power);