JavaScript provides a set of arithmetic operators that allow you to perform mathematical operations such as addition, subtraction, multiplication, and division.
These operators are crucial for various calculations in JavaScript programming.
Here’s a breakdown of each arithmetic operator with multiple examples to help you understand how to use them in your code.
1. Addition (+)
The addition operator (+) adds two numbers or concatenates strings.
Example 1: Basic Addition
let a = 5; let b = 3; let sum = a + b; console.log(sum); // Output: 8
Explanation
The + operator adds 5 and 3 and stores the result (8) in the variable sum.
Example 2: String Concatenation Using +
let firstName = 'John'; let lastName = 'Doe'; let fullName = firstName + ' ' + lastName; console.log(fullName); // Output: "John Doe"
Explanation
The + operator is used here to concatenate strings and combine firstName and lastName.
2. Subtraction (-)
The subtraction operator (-) subtracts one number from another.
Example: Basic Subtraction
let a = 10; let b = 4; let difference = a - b; console.log(difference); // Output: 6
Explanation
The – operator subtracts 4 from 10, resulting in 6.
3. Multiplication (*)
The multiplication operator (*) multiplies two numbers.
Example: Basic Multiplication
let a = 7; let b = 3; let product = a * b; console.log(product); // Output: 21
Explanation
The * operator multiplies 7 by 3 and stores the result (21) in the variable product.
4. Division (/)
The division operator (/) divides one number by another.
Example: Basic Division
let a = 20; let b = 4; let quotient = a / b; console.log(quotient); // Output: 5
Explanation
The / operator divides 20 by 4, resulting in 5.
5. Modulus (%)
The modulus operator (%) returns the remainder of a division.
Example: Using Modulus
let a = 17; let b = 5; let remainder = a % b; console.log(remainder); // Output: 2
Explanation
17 % 5 returns the remainder of 17 divided by 5, which is 2.
6. Exponentiation (**)
The exponentiation operator (**) raises the first operand to the power of the second operand.
Example: Using Exponentiation
let base = 4; let exponent = 3; let result = base ** exponent; console.log(result); // Output: 64
Explanation
The ** operator calculates 4 raised to the power of 3 (4 * 4 * 4), resulting in 64.
7. Increment (++)
The increment operator (++) increases the value of a variable by 1. It can be used in two ways: prefix (++x) and postfix (x++).
Example 1: Prefix Increment
let x = 5; let y = ++x; // x is incremented before assignment to y console.log(x); // Output: 6 console.log(y); // Output: 6
Explanation
++x increments x by 1 before assigning its value to y.
Example 2: Postfix Increment
let x = 5; let y = x++; // x is assigned to y, then incremented console.log(x); // Output: 6 console.log(y); // Output: 5
Explanation
x++ assigns the current value of x to y first, then increments x.
8. Decrement (–)
The decrement operator (–) decreases the value of a variable by 1. Like increment, it can be used as prefix (–x) and postfix (x–).
Example 1: Prefix Decrement
let x = 5; let y = --x; // x is decremented before assignment to y console.log(x); // Output: 4 console.log(y); // Output: 4
Explanation
–x decrements x by 1 before assigning its value to y.
Example 2: Postfix Decrement
let x = 5; let y = x--; // x is assigned to y, then decremented console.log(x); // Output: 4 console.log(y); // Output: 5
Explanation
x– assigns the current value of x to y first, then decrements x.
9. Combined Assignment Operators
These operators perform an operation and assign the result to a variable in a single step.
Example 1: Addition Assignment (+=)
let x = 10; x += 5; // Equivalent to x = x + 5 console.log(x); // Output: 15 Example 2: Multiplication Assignment (*=)
let y = 4; y *= 3; // Equivalent to y = y * 3 console.log(y); // Output: 12
Explanation
The += and *= operators combine addition and multiplication with assignment, respectively.
Summary
Here is a quick overview of the arithmetic operators we’ve covered:
Operator | Description | Example | Output |
---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 10 - 4 |
6 |
* |
Multiplication | 7 * 3 |
21 |
/ |
Division | 20 / 4 |
5 |
% |
Modulus (Remainder) | 17 % 5 |
2 |
** |
Exponentiation | 4 ** 3 |
64 |
++ |
Increment | ++x |
x + 1 |
-- |
Decrement | --x |
x - 1 |
By understanding and using these arithmetic operators, you can perform various mathematical calculations and operations efficiently in your JavaScript programs.