Lesson 5: JavaScript Arithmetic Operations
Master JavaScript arithmetic operations with interactive examples and real-world applications.
5.1 Arithmetic Operations in JavaScript
JavaScript provides a comprehensive set of arithmetic operators that allow you to perform mathematical operations. These operators work with numbers (both integers and floating-point values) and can be used for calculations in your programs.
Basic Arithmetic Operators
JavaScript supports the following fundamental arithmetic operators:
Addition (+)
Adds two numbers together
let sum = 10 + 5; // 15
Subtraction (-)
Subtracts one number from another
let diff = 10 - 5; // 5
Multiplication (*)
Multiplies two numbers
let product = 10 * 5; // 50
Division (/)
Divides one number by another
let quotient = 10 / 5; // 2
Modulus (%)
Returns the division remainder
let remainder = 10 % 3; // 1
Exponentiation (**)
Raises a number to a power
let power = 2 ** 3; // 8
5.2 Interactive Arithmetic Demos
Try these interactive demos to see JavaScript arithmetic in action:
Basic Calculator
Operator Precedence Demo
JavaScript follows specific rules for the order of operations. Try these examples to see how precedence works:
Example 1: 2 + 3 * 4
Example 2: (2 + 3) * 4
Example 3: 10 / 2 ** 2
5.3 Advanced Operators
JavaScript also provides compound assignment operators and increment/decrement operators for more concise code:
Compound Assignment Operators
let x = 10; // Addition assignment x += 5; // x = x + 5 → 15 // Subtraction assignment x -= 3; // x = x - 3 → 12 // Multiplication assignment x *= 2; // x = x * 2 → 24 // Division assignment x /= 4; // x = x / 4 → 6 // Modulus assignment x %= 4; // x = x % 4 → 2 // Exponentiation assignment x **= 3; // x = x ** 3 → 8
Increment and Decrement Operators
let count = 5; // Postfix increment count++; // Returns 5, then sets count to 6 // Prefix increment ++count; // Sets count to 7, then returns 7 // Postfix decrement count--; // Returns 7, then sets count to 6 // Prefix decrement --count; // Sets count to 5, then returns 5
Increment/Decrement Demo
See how increment and decrement operators work:
5.4 Practical Applications
Arithmetic operations are fundamental to many real-world applications. Here are some practical examples:
Example 5.4(a): Calculating Area and Perimeter
// Calculate rectangle area and perimeter function calculateRectangle() { const width = parseFloat(prompt("Enter rectangle width:")); const height = parseFloat(prompt("Enter rectangle height:")); const area = width * height; const perimeter = 2 * (width + height); return `Area: ${area}, Perimeter: ${perimeter}`; } // Calculate circle area and circumference function calculateCircle() { const radius = parseFloat(prompt("Enter circle radius:")); const PI = Math.PI; const area = PI * radius ** 2; const circumference = 2 * PI * radius; return `Area: ${area.toFixed(2)}, Circumference: ${circumference.toFixed(2)}`; }
Try it yourself:
Example 5.4(b): Financial Calculations
// Calculate compound interest function calculateInterest() { const principal = parseFloat(prompt("Principal amount:")); const rate = parseFloat(prompt("Annual interest rate (%):")); const years = parseInt(prompt("Number of years:")); const compounds = parseInt(prompt("Compounds per year:")); const r = rate / 100; const amount = principal * (1 + r/compounds) ** (compounds * years); const interest = amount - principal; return `Total amount: $${amount.toFixed(2)}, Interest earned: $${interest.toFixed(2)}`; } // Calculate loan payments function calculateLoanPayment() { const loanAmount = parseFloat(prompt("Loan amount:")); const interestRate = parseFloat(prompt("Annual interest rate (%):")); const loanTerm = parseInt(prompt("Loan term (years):")); const r = interestRate / 100 / 12; // Monthly interest rate const n = loanTerm * 12; // Total months // Monthly payment formula const monthlyPayment = loanAmount * r * (1 + r) ** n / ((1 + r) ** n - 1); return `Monthly payment: $${monthlyPayment.toFixed(2)}`; }