JavaScript arithmetic operators are used to perform mathematical calculations on numbers. These operators include addition, subtraction, multiplication, division, modulus, increment, decrement, and exponentiation. This article covers each arithmetic operator in detail with code examples to illustrate their use.
1. Addition Operator (+)
The addition operator adds two numbers together.
Example:
let a = 10;
let b = 5;
let result = a + b;
console.log(result); // Outputs: 15
Example: You can also use the addition operator to concatenate strings.
let firstName = 'John';
let lastName = 'Doe';
let fullName = firstName + ' ' + lastName;
console.log(fullName); // Outputs: John Doe
2. Subtraction Operator (-)
The subtraction operator subtracts the second number from the first number.
Example:
let a = 10;
let b = 5;
let result = a - b;
console.log(result); // Outputs: 5
3. Multiplication Operator (*)
The multiplication operator multiplies two numbers.
Example:
let a = 10;
let b = 5;
let result = a * b;
console.log(result); // Outputs: 50
4. Division Operator (/)
The division operator divides the first number by the second number.
Example:
let a = 10;
let b = 5;
let result = a / b;
console.log(result); // Outputs: 2
5. Modulus Operator (%)
The modulus operator returns the remainder of the division of the first number by the second number.
Example:
let a = 10;
let b = 3;
let result = a % b;
console.log(result); // Outputs: 1
Example: The modulus operator is useful for finding out if a number is even or odd.
let number = 10;
let isEven = number % 2 === 0;
console.log(isEven); // Outputs: true
6. Exponentiation Operator (**)
The exponentiation operator raises the first number to the power of the second number.
Example:
let base = 2;
let exponent = 3;
let result = base ** exponent;
console.log(result); // Outputs: 8
7. Increment Operator (++)
The increment operator increases the value of a variable by one. It can be used in both prefix and postfix forms.
Prefix:
let a = 5;
let result = ++a;
console.log(result); // Outputs: 6
console.log(a); // Outputs: 6
Postfix:
let a = 5;
let result = a++;
console.log(result); // Outputs: 5
console.log(a); // Outputs: 6
8. Decrement Operator (--)
The decrement operator decreases the value of a variable by one. It can also be used in both prefix and postfix forms.
Prefix:
let a = 5;
let result = --a;
console.log(result); // Outputs: 4
console.log(a); // Outputs: 4
Postfix:
let a = 5;
let result = a--;
console.log(result); // Outputs: 5
console.log(a); // Outputs: 4
Using Arithmetic Operators with Variables
Arithmetic operators can be used with variables to perform more complex calculations.
Example:
let a = 10;
let b = 5;
let c = 3;
let result = (a + b) * c;
console.log(result); // Outputs: 45
Combining Different Operators
You can combine different arithmetic operators in a single expression to perform multiple calculations.
Example:
let a = 10;
let b = 5;
let c = 2;
let result = a + b * c - a / b;
console.log(result); // Outputs: 18
In the above example, the multiplication and division are performed first due to operator precedence, followed by addition and subtraction.