JavaScript Arithmetic Operators
JavaScript Arithmetic
JavaScript arithmetic operators are used to perform mathematical operations on numbers. Whether you're adding, subtracting, or calculating powers, understanding how these operators work will help you manipulate numerical data efficiently.
Arithmetic Operations in JavaScript
Arithmetic operations can be applied to:
- Literals: Direct numbers like
100 + 50 - Variables: Using values stored in variables
- Expressions: Combinations of literals, variables, and other expressions
Example: Using Literals
1 2let result = 120 + 80; console.log(result); // Output: 200
Example: Using Variables
1 2 3 4let a = 15; let b = 10; let total = a + b; console.log(total); // Output: 25
Example: Using Expressions
1 2 3let multiplier = 4; let totalCost = (20 + 30) * multiplier; console.log(totalCost); // Output: 200
Operators and Operands
In arithmetic, operands are the numbers or variables that the operation is performed on, while the operator defines the type of operation.
Example:
1let sum = 10 + 5; // 10 and 5 are operands, + is the operator
Common Arithmetic Operations
1. Addition (+)
Adds two numbers or concatenates strings.
Example:
1 2 3 4let apples = 7; let oranges = 3; let totalFruits = apples + oranges; console.log(totalFruits); // Output: 10
2. Subtraction (
Subtracts one number from another.
Example:
1 2 3 4let initialStock = 50; let sold = 15; let remainingStock = initialStock - sold; console.log(remainingStock); // Output: 35
3. Multiplication (*)
Multiplies two numbers.
Example:
1 2 3 4let pricePerItem = 20; let quantity = 3; let totalPrice = pricePerItem * quantity; console.log(totalPrice); // Output: 60
4. Division (/)
Divides one number by another.
Example:
1 2 3 4let totalMarks = 450; let numberOfSubjects = 5; let averageMarks = totalMarks / numberOfSubjects; console.log(averageMarks); // Output: 90
5. Modulus (%)
Returns the remainder of a division.
Example:
1 2 3 4let totalCookies = 22; let cookiesPerBox = 6; let remainingCookies = totalCookies % cookiesPerBox; console.log(remainingCookies); // Output: 4
6. Increment (++)
Increases a number by one.
Example:
1 2 3let counter = 0; counter++; console.log(counter); // Output: 1
7. Decrement (--)
Decreases a number by one.
Example:
1 2 3let countdown = 10; countdown--; console.log(countdown); // Output: 9
8. Exponentiation (**)
Raises the first operand to the power of the second operand.
Example:
1 2 3 4let base = 3; let power = 4; let result = base ** power; // Equivalent to Math.pow(3, 4) console.log(result); // Output: 81
Operator Precedence
Operator precedence determines the order in which operations are performed. Higher precedence operators are executed before lower precedence operators.
Example:
1 2let result = 100 + 50 * 2; console.log(result); // Output: 200
Explanation: Multiplication has higher precedence, so 50 * 2 is calculated first, then added to 100.
Changing Precedence with Parentheses
Parentheses can be used to override the default precedence.
Example:
1 2let result = (100 + 50) * 2; console.log(result); // Output: 300
Explanation: The operation inside the parentheses (100 + 50) is calculated first.
Left-to-Right Execution
When multiple operations have the same precedence, they are executed from left to right.
Example:
1 2let result = 100 - 20 + 5; console.log(result); // Output: 85
Frequently Asked Questions
JavaScript arithmetic operators perform mathematical operations like addition, subtraction, multiplication, division, and modulus on numeric values.
An arithmetic operator is a symbol used to perform basic math operations, such as + for addition, - for subtraction, * for multiplication, and / for division.
When 3 + '3' is executed, JavaScript converts the number into a string, resulting in '33' instead of 6, due to type coercion.
JavaScript follows the BODMAS rule (Brackets, Orders, Division/Multiplication, Addition/Subtraction) to determine the order of arithmetic operations.
An arithmetic expression in JavaScript is any mathematical computation using numbers and arithmetic operators, such as 5 + 3 * 2, which evaluates to 11.
Still have questions?Contact our support team