DEV Community

Code WithDhanian
Code WithDhanian

Posted on

Understanding JavaScript Arithmetic

Arithmetic is a fundamental concept in JavaScript programming. It refers to performing basic mathematical operations such as addition, subtraction, multiplication, division, and more complex computations like finding the remainder or raising numbers to powers. Mastering these operations is crucial for solving real-world problems and building dynamic web applications.

In this article, we will explore JavaScript arithmetic operators in depth, with clear examples and best practices.

Basic Arithmetic Operators

JavaScript provides a set of operators to perform standard arithmetic calculations. Here is a table summarizing them:

Operator Description Example (let x = 10, y = 3) Result
+ Addition x + y 13
- Subtraction x - y 7
* Multiplication x * y 30
/ Division x / y 3.333...
% Modulus (Remainder) x % y 1
** Exponentiation (Power) x ** y 1000
++ Increment (Add 1) x++ 11 (after increment)
-- Decrement (Subtract 1) x-- 9 (after decrement)

Let us now walk through each of these operators with deeper examples.


Addition (+)

Addition adds two numbers and returns the result.

let price = 150;
let tax = 30;
let totalPrice = price + tax;
console.log(totalPrice); // 180
Enter fullscreen mode Exit fullscreen mode

Addition can also be used to concatenate strings:

let firstName = "John";
let lastName = "Doe";
let fullName = firstName + " " + lastName;
console.log(fullName); // John Doe
Enter fullscreen mode Exit fullscreen mode

Important Note: When one of the operands is a string, JavaScript converts the other operand to a string, resulting in concatenation rather than arithmetic addition.


Subtraction (-)

Subtraction finds the difference between two numbers.

let availableTickets = 100;
let soldTickets = 35;
let remainingTickets = availableTickets - soldTickets;
console.log(remainingTickets); // 65
Enter fullscreen mode Exit fullscreen mode

Multiplication (*)

Multiplication returns the product of two numbers.

let length = 12;
let width = 8;
let area = length * width;
console.log(area); // 96
Enter fullscreen mode Exit fullscreen mode

Multiplication is also used in formulas such as calculating interest:

let principal = 5000;
let rate = 0.05;
let interest = principal * rate;
console.log(interest); // 250
Enter fullscreen mode Exit fullscreen mode

Division (/)

Division returns the quotient of two numbers.

let totalAmount = 1000;
let numberOfPeople = 4;
let amountPerPerson = totalAmount / numberOfPeople;
console.log(amountPerPerson); // 250
Enter fullscreen mode Exit fullscreen mode

It is important to note that if the division results in a non-integer, JavaScript will return a floating-point number.


Modulus (%)

The modulus operator returns the remainder of a division operation.

let minutes = 135;
let minutesInHour = 60;
let remainingMinutes = minutes % minutesInHour;
console.log(remainingMinutes); // 15
Enter fullscreen mode Exit fullscreen mode

A common use case of the modulus operator is to check whether a number is even or odd:

let number = 9;
if (number % 2 === 0) {
  console.log("Even");
} else {
  console.log("Odd");
}
// Output: Odd
Enter fullscreen mode Exit fullscreen mode

Exponentiation (**)

Exponentiation raises the first operand to the power of the second operand.

let base = 4;
let exponent = 3;
let result = base ** exponent;
console.log(result); // 64
Enter fullscreen mode Exit fullscreen mode

This operation is useful for mathematical calculations involving powers or exponential growth.


Increment (++) and Decrement (--)

Increment and decrement operators add or subtract one from a numeric value.

let count = 5;
count++;
console.log(count); // 6

count--;
console.log(count); // 5
Enter fullscreen mode Exit fullscreen mode

These operators can be prefix or postfix:

  • Prefix (++count) increments the value first, then returns the new value.
  • Postfix (count++) returns the current value first, then increments.

Example:

let a = 2;
console.log(++a); // 3 (increment first)
console.log(a++); // 3 (then increment after)
console.log(a);   // 4 (after increment)
Enter fullscreen mode Exit fullscreen mode

Combining Operations

JavaScript follows the standard order of operations (also called operator precedence):

  1. Parentheses ()
  2. Exponentiation **
  3. Multiplication and Division *, /, %
  4. Addition and Subtraction +, -

Example:

let result = (5 + 3) * 2 ** 3 / 4;
console.log(result); // 16
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • (5 + 3) evaluates to 8
  • 2 ** 3 evaluates to 8
  • 8 * 8 equals 64
  • 64 / 4 equals 16

Conclusion

Arithmetic operations are the foundation of logical programming. JavaScript offers simple yet powerful operators for manipulating numbers and performing calculations. Understanding and using these operations efficiently is critical, whether you are calculating totals in a shopping cart, determining layout dimensions in a web app, or building financial software.

To master JavaScript even further, including building real-world projects with deep code examples, you can explore the complete JavaScript ebook available here:

Complete JavaScript eBook – Beginner to Advanced

This resource covers everything from basics to advanced topics, complete with exercises and real-world applications.

Top comments (0)