JavaScript is a versatile and powerful programming language that provides a robust set of arithmetic operations. These operations are fundamental for performing calculations and manipulating numerical data in various applications. This guide will cover the basic arithmetic operations in JavaScript, including addition, subtraction, multiplication, division, modulus, and more. We'll also discuss some advanced concepts like increment/decrement operators and how JavaScript handles floating-point arithmetic.
Basic Arithmetic Operations
JavaScript supports the following basic arithmetic operations:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulus (%)
- Addition (+)
The addition operator is used to add two numbers.
let a = 5;
let b = 10;
let sum = a + b;
console.log(sum); // Output: 15
When the addition operator is used with strings, it performs string concatenation.
let lastName = "Doe";
let fullName = firstName + " " + lastName;
console.log(fullName); // Output: John Doe
Subtraction (-)
The subtraction operator subtracts one number from another.
let b = 5;
let difference = a - b;
console.log(difference); // Output: 15
Multiplication (*)
The multiplication operator multiplies two numbers.
let b = 3;
let product = a * b;
console.log(product); // Output: 12
Division (/)
The division operator divides one number by another.
let a = 20;
let b = 4;
let quotient = a / b;
console.log(quotient); // Output: 5
Modulus (%)
The modulus operator returns the remainder of a division operation.
let b = 3;
let remainder = a % b;
console.log(remainder); // Output: 1
Increment and Decrement Operators
JavaScript provides two useful operators for incrementing and decrementing values:
- Increment (++)
- Decrement (--)
- Increment (++)
The increment operator increases a number by one. It can be used as a prefix or postfix.
a++;
console.log(a); // Output: 6
let b = 5;
console.log(++b); // Output: 6
Decrement (--)
The decrement operator decreases a number by one. It can also be used as a prefix or postfix.
a--;
console.log(a); // Output: 4
let b = 5;
console.log(--b); // Output: 4
Compound Assignment Operators
JavaScript offers compound assignment operators that combine arithmetic operations with assignment:
- Addition assignment (+=)
- Subtraction assignment (-=)
- Multiplication assignment (*=)
- Division assignment (/=)
- Modulus assignment (%=)
- Addition Assignment (+=)
a += 5;
console.log(a); // Output: 15
Subtraction Assignment (-=)
a -= 3;
console.log(a); // Output: 7
Multiplication Assignment (*=)
a *= 2;
console.log(a); // Output: 20
Division Assignment (/=)
a /= 2;
console.log(a); // Output: 5
Modulus Assignment (%=)
a %= 3;
console.log(a); // Output: 1
Advanced Concepts
Exponentiation (**)
JavaScript also supports the exponentiation operator (**) for raising a number to the power of another number.
let a = 2;
let b = 3;
let result = a ** b;
console.log(result); // Output: 8
Floating-Point Arithmetic
JavaScript handles both integers and floating-point numbers. However, due to the binary representation of floating-point numbers, you might encounter precision issues.
let a = 0.1;
let b = 0.2;
let sum = a + b;
console.log(sum); // Output: 0.30000000000000004
To handle precision issues, you can use the toFixed method.
console.log(sumFixed); // Output: 0.30
Math Object
JavaScript provides the Math object for more advanced mathematical operations.
Math.sqrt
let squareRoot = Math.sqrt(num);
console.log(squareRoot); // Output: 4
Math.pow
let exponent = 3;
let power = Math.pow(base, exponent);
console.log(power); // Output: 8
Math.round, Math.ceil, and Math.floor
console.log(Math.round(num)); // Output: 6
console.log(Math.ceil(num)); // Output: 6
console.log(Math.floor(num)); // Output: 5
Random Numbers
The Math.random method generates a random number between 0 and 1.
console.log(randomNum); // Output: a random number between 0 and 1
To generate a random number within a specific range:
let min = 1;
let max = 10;
let randomNumInRange = Math.floor(Math.random() * (max - min + 1)) + min;
console.log(randomNumInRange); // Output: a random number between 1 and 10
Practical Examples
- Calculating the Area of a Circle
- To calculate the area of a circle given its radius:
let area = Math.PI * radius ** 2;
console.log(area); // Output: 78.53981633974483
Converting Fahrenheit to Celsius
To convert a temperature from Fahrenheit to Celsius:
let celsius = (fahrenheit - 32) * (5 / 9);
console.log(celsius); // Output: 37.77777777777778
Simple Interest Calculation
To calculate simple interest:
let rate = 5;
let time = 2;
let interest = (principal * rate * time) / 100;
console.log(interest); // Output: 100
JavaScript provides a comprehensive set of arithmetic operations, from basic addition and subtraction to more complex calculations involving exponentiation and the Math object. Understanding these operations and how to apply them is crucial for performing calculations and developing dynamic applications.
Practice Excercise Practice now