TL ; DR:
We can tell computers to perform calculations for us using +
, -
, *
, /
:
console.log(2 + 6); // Prints 8
console.log(10 - 7); // Prints 3
console.log(3 * 3); // Prints 9
console.log(6 / 2); // Prints 3
We can also use variables. In these examples, JavaScript will replace variable num
with its value 6
to do the calculations:
let num = 6;
console.log(num + 5); // Prints 11
console.log(10 - num); // Prints 4
console.log(3 * num); // Prints 18
console.log(num / 2); // Prints 3
Full lesson:
Computers absolutely excel at performing calculations. The "compute" in their name comes from their historical association with providing answers to mathematical questions.
These calculations can be performed either directly with numbers or with variables that are assigned numeric values.
Arithmetic operators
Arithmetic operators are used with numeric values to perform common mathematical operations:
Operator | Name | Examples |
---|---|---|
+ | Addition | 3 + 5, x + 5, 3 + y, x + y |
- | Subtraction | 8 - 3, 8 - y, x - 3, x - y |
* | Multiplication | 3 * 5, 3 * y, x * 5, x * y |
/ | Division | 8 / 4, 8 / y, x / 8, x / y |
% | Modulus | 8 % 4, 8 % y, x % 4, x % y |
** | Exponentiation | 3 ** 2, 3 ** y, x ** 3, x ** y |
Note that performing arithmetic on variables does not change the variable - you can only update a variable using the =
sign.
Let's go through some examples together to better understand how each operator works:
Addition
// Declare 2 variables and initialize them:
let a = 2;
let b = 3;
console.log(a + b); // Output: 5
console.log(a + 10); // Output: 12
Subtraction
// Declare 2 variables and initialize them:
let a = 2;
let b = 3;
console.log(a - b); // Output: -1
console.log(10 - b); // Output: 7
Multiplication
// Declare 2 variables and initialize them:
let a = 2;
let b = 3;
console.log(a * 5); // Output: 10
console.log(-2 * a * b); // Output: -12
Division
// Declare 2 variables and initialize them:
let a = 2;
let b = 3;
console.log(b / a); // Output: 1.5
console.log(a / 10); // Output: 0.2
Exponentiation
The **
operator performs the exponential calculation (2**3 translates to 2 * 2 * 2):
// Declare 2 variables and initialize them:
let a = 2;
let b = 3;
console.log(2 ** 3); // Output: 8
console.log(b ** a); // Output: 9
We left the modulo operator (%) for the next lesson.
Assignment
Follow the Coding Tutorial and let's practice with arithmetic operators!
Hint
Look at the examples above if you get stuck.
Arithmetic operators are fundamental in programming, allowing us to perform basic mathematical operations. These operators are essential for tasks ranging from simple calculations to complex algorithms. Understanding how to use arithmetic operators effectively is crucial for any programmer.
Common scenarios where arithmetic operators are useful include financial calculations, game development, data analysis, and any application that requires mathematical computations.
Before diving into complex operations, it's important to grasp the basic arithmetic operators:
+
(Addition): Adds two numbers.-
(Subtraction): Subtracts the second number from the first.*
(Multiplication): Multiplies two numbers./
(Division): Divides the first number by the second.%
(Modulus): Returns the remainder of the division of two numbers.**
(Exponentiation): Raises the first number to the power of the second.Understanding these basics is essential before moving on to more complex operations and algorithms.
Let's explore the key concepts and techniques involved in using arithmetic operators:
Here's an example of using variables in arithmetic operations:
let x = 10;
let y = 5;
console.log(x + y); // Output: 15
console.log(x - y); // Output: 5
console.log(x * y); // Output: 50
console.log(x / y); // Output: 2
console.log(x % y); // Output: 0
console.log(x ** y); // Output: 100000
Let's look at some examples to see how arithmetic operators are used in various contexts:
let length = 10;
let width = 5;
let area = length * width;
console.log(area); // Output: 50
let celsius = 30;
let fahrenheit = (celsius * 9/5) + 32;
console.log(fahrenheit); // Output: 86
let principal = 1000;
let rate = 5;
let time = 2;
let compoundInterest = principal * (1 + rate/100) ** time;
console.log(compoundInterest); // Output: 1102.5
When working with arithmetic operators, it's important to avoid common mistakes and follow best practices:
Here's an example of using parentheses to ensure correct calculations:
let result = (2 + 3) * 4; // Output: 20
Once you're comfortable with the basics, you can explore advanced techniques such as:
Here's an example of chaining operations:
let result = (2 + 3) * (4 - 1) / 2; // Output: 7.5
Let's implement a function that uses arithmetic operators to calculate the average of an array of numbers:
// Function to calculate the average of an array of numbers
function calculateAverage(numbers) {
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i]; // Add each number to the sum
}
return sum / numbers.length; // Divide the sum by the number of elements
}
// Example usage
let numbers = [10, 20, 30, 40, 50];
console.log(calculateAverage(numbers)); // Output: 30
Debugging and testing are crucial for ensuring your code works correctly:
Here's an example of a test case for the calculateAverage function:
// Test case for calculateAverage function
let testNumbers = [5, 10, 15];
console.log(calculateAverage(testNumbers)); // Expected output: 10
When solving problems related to arithmetic operations, consider the following strategies:
In this lesson, we covered the basics of arithmetic operators in JavaScript, explored various examples and use cases, and discussed best practices and advanced techniques. Mastering these concepts is essential for any programmer, as arithmetic operations are fundamental to many programming tasks.
Keep practicing and exploring further applications to strengthen your understanding and skills.
For further reading and practice, check out the following resources:
Our interactive tutorials and AI-assisted learning will help you master problem-solving skills and teach you the algorithms to know for coding interviews.
Start Coding for FREE