JavaScript Arithmetic and Assignment Operators
JavaScript arithmetic and assignment operators are fundamental tools for mathematical calculations and efficient variable updates. This guide covers addition, subtraction, multiplication, division, modulus, increment/decrement, and compound assignment operators with practical examples.
JavaScript arithmetic and assignment operators are fundamental building blocks that you'll use in virtually every program you write. These operators let you perform mathematical calculations and modify variable values efficiently. Understanding them well will make your JavaScript journey much smoother.
Arithmetic Operators: The Mathematical Foundation
JavaScript provides seven main arithmetic operators for performing mathematical operations on numbers:
let a = 10;
let b = 3;
console.log(a + b); // Addition: 13
console.log(a - b); // Subtraction: 7
console.log(a * b); // Multiplication: 30
console.log(a / b); // Division: 3.3333...
console.log(a % b); // Modulus (remainder): 1
console.log(a ** b); // Exponentiation: 1000
console.log(++a); // Increment: 11
console.log(--b); // Decrement: 2The modulus operator (%) deserves special attention. It returns the remainder after division, which is incredibly useful for tasks like checking if a number is even or odd:
let number = 15;
if (number % 2 === 0) {
console.log("Even");
} else {
console.log("Odd"); // This will execute
}Increment and Decrement: Pre vs Post
The increment (++) and decrement (--) operators have two forms that behave differently:
let x = 5;
console.log(x++); // Post-increment: prints 5, then x becomes 6
console.log(x); // Now x is 6
let y = 5;
console.log(++y); // Pre-increment: y becomes 6, then prints 6
console.log(y); // Still 6Assignment Operators: Efficient Value Updates
Assignment operators combine arithmetic operations with variable assignment, making your code more concise and readable:
let score = 100;
// Basic assignment
score = 150;
// Compound assignment operators
score += 25; // Same as: score = score + 25; (175)
score -= 10; // Same as: score = score - 10; (165)
score *= 2; // Same as: score = score * 2; (330)
score /= 3; // Same as: score = score / 3; (110)
score %= 7; // Same as: score = score % 7; (5)
score **= 2; // Same as: score = score ** 2; (25)
console.log(score); // 25Operator Precedence: Order Matters
JavaScript follows mathematical order of operations. Multiplication and division happen before addition and subtraction:
let result1 = 5 + 3 * 2; // 11, not 16
let result2 = (5 + 3) * 2; // 16, parentheses change the order
console.log(result1); // 11
console.log(result2); // 16When operators have the same precedence, JavaScript evaluates from left to right:
let calculation = 20 / 4 * 2; // (20 / 4) * 2 = 10, not 20 / (4 * 2)Practical Examples
Here's how these operators work together in real scenarios:
// Calculate total price with tax
let price = 99.99;
let taxRate = 0.08;
price *= (1 + taxRate); // price = price * 1.08
console.log(`Total: $${price.toFixed(2)}`); // Total: $107.99
// Track game score
let playerScore = 0;
playerScore += 50; // Completed level
playerScore += 25; // Bonus points
playerScore -= 10; // Penalty
console.log(`Final score: ${playerScore}`); // Final score: 65
// Check if user can access content (age verification)
let userAge = 17;
let canAccess = userAge >= 18;
console.log(`Access granted: ${canAccess}`); // Access granted: falseCommon Pitfalls to Avoid
Watch out for these common mistakes:
// Division by zero returns Infinity, not an error
console.log(10 / 0); // Infinity
// Modulus with decimals can be unexpected
console.log(5.5 % 2); // 1.5, not what you might expect
// Increment/decrement with strings
let counter = "5";
counter++; // counter becomes 6 (number), not "6" (string)What's Next
Now that you understand arithmetic and assignment operators, you're ready to explore comparison operators. These will let you compare values and make decisions in your programs, forming the foundation for conditional statements and loops that make JavaScript truly powerful.