Arithmetic Operators in JavaScript: Explained with Examples
If you're new to programming, you might be wondering what arithmetic operators are and how they work in JavaScript.
Arithmetic operators are used to perform basic mathematical calculations on numbers, such as addition, subtraction, multiplication, and division.
In this article, we'll cover the basics of arithmetic operators in JavaScript and provide some examples to help you understand how they work.
Table of Contents
- Introduction
- Addition
- Subtraction
- Multiplication
- Division
- Modulus
- Order of Operations
- Examples
- Conclusion
- FAQs
Introduction
In JavaScript, arithmetic operators are used to perform mathematical calculations on numeric values. There are several arithmetic operators in JavaScript, including addition, subtraction, multiplication, division, and modulo.
Addition
The addition operator (+) is used to add two numbers together. For example:
let x = 5;
let y = 3;
let z = x + y; // z is 8
In the example above, the value of x is 5, the value of y is 3, and the value of z is the result of adding x and y, which is 8.
Subtraction
The subtraction operator (-) is used to subtract one number from another. For example:
let x = 5;
let y = 3;
let z = x - y; // z is 2
In the example above, the value of x is 5, the value of y is 3, and the value of z is the result of subtracting y from x, which is 2.
Multiplication
The multiplication operator (*) is used to multiply two numbers together. For example:
let x = 5;
let y = 3;
let z = x * y; // z is 15
In the example above, the value of x is 5, the value of y is 3, and the value of z is the result of multiplying x and y, which is 15.
Division
The division operator (/) is used to divide one number by another. For example:
let x = 6;
let y = 3;
let z = x / y; // z is 2
In the example above, the value of x is 6, the value of y is 3, and the value of z is the result of dividing x by y, which is 2.
Modulus
The modulus operator (%) is used to find the remainder when one number is divided by another. For example:
let x = 7;
let y = 3;
let z = x % y; // z is 1
In the example above, the value of x is 7, the value of y is 3, and the value of z is the remainder when x is divided by y, which is 1.
Order of Operations
In JavaScript, the order of operations determines the sequence in which arithmetic operations are performed. This is important to know, as it can affect the outcome of your calculations.
The order of operations in JavaScript is as follows:
- Parentheses
- Exponents
- Multiplication and division (from left to right)
- Addition and subtraction (from left to right)
This means that when you have a complex expression with multiple operators, JavaScript will perform the calculations in the order specified above. If you want to change the order of operations, you can use parentheses to group parts of the expression together.
Let's take a look at an example:
let result = 5 + 2 * 3; // result is 11, not 21
In this example, JavaScript performs the multiplication first, then the addition. This is because multiplication has a higher precedence than addition.
If we want to change the order of operations, we can use parentheses:
let result = (5 + 2) * 3; // result is 21
In this example, we're using parentheses to group the addition operation together. This tells JavaScript to perform the addition first, then the multiplication.
Examples
Now let's take a look at some examples to see how arithmetic operators work in JavaScript:
Example 1: Simple Calculation
let x = 5;
let y = 3;
let z = (x + y) * 2; // z is 16
In this example, we're using the addition operator to add x and y together, then multiplying the result by 2 to get the value of z.
Example 2: Using Modulus
let x = 10;
let y = 3;
let z = x % y; // z is 1
In this example, we're using the modulus operator to find the remainder when x is divided by y.
Example 3: Order of Operations
let x = 5;
let y = 3;
let z = x + y * 2; // z is 11
In this example, we're using the multiplication operator to multiply y by 2 first, then adding the result to x using the addition operator.
You can also combine arithmetic operators in JavaScript to perform more complex calculations. Let's take a look at an example:
let result = ((5 + 2) * 3) / 2 - 1; // result is 9.5
In this example, we're using parentheses to group the addition operation together, then multiplying the result by 3, dividing by 2, and subtracting 1.
Conclusion
Arithmetic operators are essential for performing mathematical calculations in JavaScript. In this article, we've covered the basics of arithmetic operators, including addition, subtraction, multiplication, division, modulo, and the order of operations. We've also provided examples to help you understand how these operators work in practice.
FAQs
What are arithmetic operators in JavaScript?
Arithmetic operators in JavaScript are used to perform basic mathematical calculations on numbers, such as addition, subtraction, multiplication, division, and modulus.
What is the order of operations in JavaScript?
The order of operations in JavaScript is as follows: parentheses, exponents, multiplication and division (from left to right), and addition and subtraction (from left to right).
What is the modulo operator in JavaScript?
The modulo/modulus operator (%) in JavaScript is used to find the remainder when one number is divided by another.
How do you perform a simple calculation in JavaScript?
You can perform a simple calculation in JavaScript using arithmetic operators. For example, to add two numbers together, you can use the addition operator (+).
Can you use arithmetic operators on non-numeric values in JavaScript?
No, arithmetic operators can only be used on numeric values in JavaScript. If you try to use them on non-numeric values, you'll get a NaN (Not a Number) result.