Introduction
Arithmetic Operators are used to perform mathematical calculations on numeric values. They allow you to add, subtract, multiply, divide, find the remainder, increment, decrement, and calculate powers of numbers.
Arithmetic operators are among the most commonly used operators in JavaScript and are essential for performing calculations in web applications, games, financial software, and automation scripts.
For automation engineers, arithmetic operators are useful for calculating totals, validating data, processing API responses, working with dates and times, generating dynamic test data, and performing various mathematical operations.
What are Arithmetic Operators?
Arithmetic Operators are operators that perform mathematical operations on one or more operands.
Example:
let a = 10;
let b = 5;
console.log(a + b);
Output
15
Why Do We Need Arithmetic Operators?
Arithmetic operators help us:
Perform mathematical calculations.
Calculate totals and averages.
Modify numeric values.
Generate dynamic test data.
Process API responses.
Validate calculations in automation tests.
Types of Arithmetic Operators
| Operator | Description | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (Remainder) | a % b |
** | Exponentiation (Power) | a ** b |
++ | Increment | a++ |
-- | Decrement | a-- |
Addition (+)
The addition operator adds two values.
let a = 20;
let b = 15;
console.log(a + b);
Output
35
Addition with Variables
let price = 1200;
let tax = 300;
let total = price + tax;
console.log(total);
Output
1500
Subtraction (-)
The subtraction operator subtracts one value from another.
let a = 20;
let b = 8;
console.log(a - b);
Output
12
Multiplication (*)
The multiplication operator multiplies two values.
let length = 12;
let width = 5;
console.log(length * width);
Output
60
Division (/)
The division operator divides one value by another.
let total = 100;
let students = 4;
console.log(total / students);
Output
25
Modulus (%)
The modulus operator returns the remainder after division.
let number = 17;
console.log(number % 5);
Output
2
Practical Example
Check whether a number is even.
let number = 24;
console.log(number % 2);
Output
0
A remainder of 0 indicates that the number is even.
Exponentiation (**)
The exponentiation operator raises a number to a specified power.
console.log(2 ** 4);
Output
16
This is equivalent to:
2 × 2 × 2 × 2
Increment (++)
The increment operator increases a variable by 1.
let count = 5;
count++;
console.log(count);
Output
6
Pre-Increment
The value is increased before it is used.
let a = 10;
console.log(++a);
Output
11
Post-Increment
The value is used first and then increased.
let a = 10;
console.log(a++);
console.log(a);
Output
10
11
Decrement (--)
The decrement operator decreases a variable by 1.
let count = 10;
count--;
console.log(count);
Output
9
Pre-Decrement
let a = 8;
console.log(--a);
Output
7
Post-Decrement
let a = 8;
console.log(a--);
console.log(a);
Output
8
7
Arithmetic with Strings
The + operator concatenates strings.
console.log("Hello " + "World");
Output
Hello World
If one operand is a string, JavaScript converts the other operand into a string.
console.log("10" + 5);
Output
105
Other arithmetic operators convert strings into numbers whenever possible.
console.log("20" - 5);
Output
15
Operator Precedence
JavaScript follows the standard mathematical order of operations.
console.log(10 + 5 * 2);
Output
20
Multiplication is performed before addition.
Using parentheses changes the order.
console.log((10 + 5) * 2);
Output
30
Real-World Example
Suppose an online shopping application calculates the total amount.
let productPrice = 1500;
let deliveryCharge = 100;
let totalAmount = productPrice + deliveryCharge;
console.log(totalAmount);
Output
1600
Automation Testing Example
Automation engineers often validate calculated values returned by APIs.
const quantity = 4;
const price = 500;
const total = quantity * price;
console.log(total);
Output
2000
You can compare this calculated value with the API response to verify its correctness.
Another example:
const passedTests = 48;
const failedTests = 2;
const totalTests = passedTests + failedTests;
console.log(totalTests);
Output
50
This can be used while generating automation test reports.
Common Mistakes
Forgetting Operator Precedence
Incorrect assumption:
console.log(10 + 5 * 2);
Output:
20
Not:
30
Use parentheses when necessary.
Confusing Addition with String Concatenation
console.log("100" + 20);
Output
10020
Convert the string to a number if mathematical addition is required.
console.log(Number("100") + 20);
Output
120
Dividing by Zero
console.log(10 / 0);
Output
Infinity
JavaScript does not throw an error, but the result is Infinity.
Best Practices
Use Meaningful Variable Names
Good example:
let totalAmount = price + tax;
Instead of:
let x = a + b;
Use Parentheses for Readability
Even when not required, parentheses make expressions easier to understand.
let total = (price + tax) * quantity;
Convert Strings Before Performing Calculations
If numeric values come from user input or APIs, convert them using Number() before performing arithmetic operations.
Avoid Unnecessary Increment and Decrement Operations
Use ++ and -- only when they improve readability.
Conclusion
Arithmetic operators are fundamental building blocks of JavaScript. They allow developers to perform mathematical calculations efficiently and are used extensively in web applications, automation scripts, financial systems, and data processing.
Understanding how each arithmetic operator works, including operator precedence and type conversion, helps you write accurate and reliable JavaScript programs.
Frequently Asked Questions (FAQs)
What are Arithmetic Operators in JavaScript?
Arithmetic operators perform mathematical calculations such as addition, subtraction, multiplication, division, modulus, exponentiation, increment, and decrement.
Which operator is used for addition?
The + operator.
Example:
console.log(10 + 5);
Output:
15
What does the modulus (%) operator do?
It returns the remainder after division.
Example:
console.log(17 % 5);
Output:
2
What is the difference between ++a and a++?
++aincreases the value before using it (pre-increment).a++uses the current value first and then increases it (post-increment).
Why are arithmetic operators important in automation testing?
Automation engineers use arithmetic operators to validate calculations, process API responses, generate reports, calculate totals, and create dynamic test data.
Key Takeaways
Arithmetic operators perform mathematical operations on numeric values.
JavaScript supports addition (
+), subtraction (-), multiplication (*), division (/), modulus (%), exponentiation (**), increment (++), and decrement (--).The
+operator also performs string concatenation.JavaScript follows operator precedence when evaluating expressions.
Parentheses can be used to control the order of evaluation.
Use
Number()to convert strings before performing calculations.Be aware of the difference between pre-increment and post-increment.
Division by zero returns
Infinity.Arithmetic operators are widely used in web development and automation testing.
Understanding arithmetic operators is essential for writing efficient and reliable JavaScript programs.
