JavaScript Arithmetic Operators (Live Playground)
Arithmetic operators are used to perform basic mathematical operations in JavaScript, such as addition, subtraction, multiplication, and division. In this tutorial, we'll explore the most commonly used arithmetic operators and learn how to use them in our code.
Addition (+
)
The addition operator is used to add two numbers together.
Example:
const a = 10;
const b = 20;
const sum = a + b;
console.log(sum); // Output: 30
Subtraction (-
)
The subtraction operator is used to subtract one number from another.
Example:
const a = 10;
const b = 20;
const difference = b - a;
console.log(difference); // Output: 10
Multiplication (*
)
The multiplication operator is used to multiply two numbers together.
Example:
const a = 10;
const b = 20;
const product = a * b;
console.log(product); // Output: 200
Division (/
)
The division operator is used to divide one number by another.
Example:
const a = 20;
const b = 10;
const quotient = a / b;
console.log(quotient); // Output: 2
Modulus (%
)
The modulus operator is used to find the remainder after dividing one number by another.
Example:
const a = 20;
const b = 3;
const remainder = a % b;
console.log(remainder); // Output: 2
Conclusion
Understanding and using arithmetic operators is crucial for performing calculations in your JavaScript programs. Familiarize yourself with the basic arithmetic operators, such as addition, subtraction, multiplication, division, and modulus, to create more complex mathematical expressions and solve real-world problems.