Arithmetic operators help you do math with numbers in your program. They let you add, subtract, multiply, and divide values easily.
0
0
Arithmetic operators in Javascript
Introduction
Calculating the total price of items in a shopping cart.
Finding the difference between two dates or times.
Increasing a score in a game after a player earns points.
Splitting a bill evenly among friends.
Converting units, like inches to centimeters.
Syntax
Javascript
result = value1 + value2; // addition result = value1 - value2; // subtraction result = value1 * value2; // multiplication result = value1 / value2; // division result = value1 % value2; // remainder (modulus) result = value1 ** value2; // exponentiation (power)
Use + for adding numbers or joining text (strings).
The % operator gives the remainder after division.
Examples
Adds 5 and 3 to get 8.
Javascript
let sum = 5 + 3; // sum is 8
Subtracts 4 from 10 to get 6.
Javascript
let difference = 10 - 4; // difference is 6
Multiplies 7 by 2 to get 14.
Javascript
let product = 7 * 2; // product is 14
Divides 10 by 3 and gives the remainder 1.
Javascript
let remainder = 10 % 3; // remainder is 1
Sample Program
This program shows how to use each arithmetic operator with two numbers, 12 and 5. It prints the result of each operation.
Javascript
const a = 12; const b = 5; console.log(`Addition: ${a} + ${b} = ${a + b}`); console.log(`Subtraction: ${a} - ${b} = ${a - b}`); console.log(`Multiplication: ${a} * ${b} = ${a * b}`); console.log(`Division: ${a} / ${b} = ${a / b}`); console.log(`Remainder: ${a} % ${b} = ${a % b}`); console.log(`Exponentiation: ${a} ** 2 = ${a ** 2}`);
OutputSuccess
Important Notes
Division / can give decimal numbers if the division is not exact.
Exponentiation ** raises a number to the power of another number.
Remember that + can also join text, so be careful when mixing numbers and strings.
Summary
Arithmetic operators let you do basic math in your code.
Use +, -, *, /, %, and ** for addition, subtraction, multiplication, division, remainder, and power.
They help solve many everyday problems like calculating totals or splitting amounts.