๐Ÿ”ข JavaScript Operators & Expressions
Estimated reading: 3 minutes 275 views

JavaScript Arithmetic Operators: A Complete Guide

Arithmetic operators are the backbone of mathematical operations in JavaScript. Whether youโ€™re building a calculator, processing user input, or handling data analytics, understanding these operators is crucial for writing clear and effective code.

In this guide, youโ€™ll learn:

  • What arithmetic operators are and why they matter in JavaScript
  • How each operator works, with line-by-line code explanations
  • Best practices and advanced usage tips for robust, modern JavaScript

Letโ€™s explore how arithmetic operators can make your code smarter and more powerful.


Core Concepts and Theory

What Are Arithmetic Operators in JavaScript?

Arithmetic operators are symbols used to perform basic mathematical operations. They are essential tools for manipulating numeric values in almost every JavaScript application.

Key Operators and Their Uses

OperatorNameExampleResult
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division6 / 32
%Modulus7 % 31
++Incrementa++a = a+1
--Decrementa--a = a-1
**Exponentiation2 ** 38

Use Cases:

  • Price calculations in shopping carts
  • Animating values like movement or scale
  • Game score updates
  • Data computations in dashboards

๐Ÿ“— Key Terms:

  • Operand: A value or variable used with an operator (e.g., in a + b, both a and b)
  • Expression: A statement that evaluates to a result (e.g., a * (b + c))

Comparison: var vs let for Arithmetic Variables

Featurevarlet
ScopeFunction / GlobalBlock
Redeclarable Yes No
Reassignable Yes Yes
Use in Math Supported Supported

Code Implementation & Examples

// Arithmetic Operators Demo โ€” W3Office Example

let a = 10;
let b = 3;

// Addition
let sum = a + b; // 13

// Subtraction
let diff = a - b; // 7

// Multiplication
let product = a * b; // 30

// Division
let quotient = a / b; // 3.333...

// Modulus
let remainder = a % b; // 1

// Exponentiation (ES6+)
let power = a ** b; // 1000

// Increment
a++; // a becomes 11

// Decrement
b--; // b becomes 2

Line-by-Line Explanation:

  • let a = 10; โžค Declares a with value 10
  • let b = 3; โžค Declares b with value 3
  • let sum = a + b; โžค Adds a and b
  • let diff = a - b; โžค Subtracts b from a
  • let product = a * b; โžค Multiplies a and b
  • let quotient = a / b; โžค Divides a by b
  • let remainder = a % b; โžค Gets remainder
  • let power = a ** b; โžค a raised to b power
  • a++; โžค Increments a by 1
  • b--; โžค Decrements b by 1

Real-Life Example:

Need to build a billing system or apply discount logic? Arithmetic operators help total item costs, calculate tax, and apply discounts.


Advanced Techniques or Best Practices

Best Practice: Use Numeric Types

Ensure operands are actual numbers to avoid coercion issues.

let total = "5" + 3; //  Output: "53"
let corrected = parseInt("5") + 3; // Output: 8

Common Pitfall: Division by Zero

let result = 10 / 0; // Returns Infinity, not an error

Safe Arithmetic Example:

function W3OfficeSafeDivide(a, b) {
if (b === 0) {
return 'Error: Division by zero';
}
return a / b;
}

ES6+ Compatibility

  • ** (Exponentiation) is part of ES6+
  • Use Math.pow(a, b) for legacy support

๏ธ Async Arithmetic Caution

Use await if values come from APIs or files:

let total = await getPrice() + await getTax();

Summary & Key Takeaways

  • Arithmetic operators are essential for numeric logic
  • Master +, -, *, /, %, ++, --, and **
  • Always sanitize input values to ensure proper math
  • Handle division by zero with custom logic
  • Choose modern (**) vs traditional (Math.pow) based on environment

FAQ Section

Q1: What are arithmetic operators in JavaScript?
Theyโ€™re symbols used to perform basic math like +, -, *, /, %, ++, --, and **.

Q2: How do you use the modulus operator?
% returns the remainder of a division: 7 % 3 equals 1.

Q3: Whatโ€™s the result of dividing by zero?
JavaScript returns Infinity, not an error.

Q4: How do I ensure two values are numbers?
Use Number(), parseInt(), or parseFloat() before performing math.

Q5: Is the exponentiation operator (**) universal?
Yes, in ES6+ browsers. Use Math.pow() if older support is needed.

Share Now :
Share

JavaScript โ€” Arithmetic Operators

Or Copy Link

CONTENTS
Scroll to Top