exponentiation javascript increment decrement javascript-tutorial javascript-basics arithmetic-operators addition subtraction multiplication
Section 2.3: Operators
Tutorial 2.3.1: Arithmetic Operators
Arithmetic Operators in JavaScript
Arithmetic operators are used to perform mathematical calculations on numeric values (literals or variables). They are fundamental for tasks ranging from simple calculations to complex algorithms.
Operator | Symbol | Description | Example | Result |
---|---|---|---|---|
Addition | + | Adds two numbers | 5 + 3 | 8 |
Subtraction | - | Subtracts one number from another | 10 - 4 | 6 |
Multiplication | * | Multiplies two numbers | 7 * 6 | 42 |
Division | / | Divides one number by another | 20 / 4 | 5 |
Modulus | % | Returns the remainder of division | 10 % 3 | 1 |
Exponentiation | ** | Raises a number to a power | 2 ** 3 | 8 |
Increment | ++ | Increases a number by 1 | let x = 5; x++ | 6 |
Decrement | -- | Decreases a number by 1 | let y = 7; y-- | 6 |
List of Arithmetic Operators
- Addition (
+
):
- Adds two numbers.
- Example:
let sum = 10 + 5; // 15
- Subtraction (
-
):
- Subtracts the second number from the first.
- Example:
let difference = 10 - 5; // 5
- Multiplication (
*
):
- Multiplies two numbers.
- Example:
let product = 10 * 5; // 50
- Division (
/
):
- Divides the first number by the second.
- Example:
let quotient = 10 / 5; // 2
- Modulus (
%
):
- Returns the remainder of the division.
- Example:
let remainder = 10 % 3; // 1
- Exponentiation (
</strong>
)**:
- Raises the first operand to the power of the second operand.
- Example:
let power = 2 ** 3; // 8
- Increment (
++
):
- Increases a variable by one.
- Postfix Increment (
i++
):
let i = 5;
console.log(i++); // Outputs 5, then i becomes 6
console.log(i); // Outputs 6
- Prefix Increment (
++i
):
let i = 5;
console.log(++i); // Outputs 6
- Decrement (
--
):
- Decreases a variable by one.
- Postfix Decrement (
i--
):
let i = 5;
console.log(i--); // Outputs 5, then i becomes 4
console.log(i); // Outputs 4
- Prefix Decrement (
--i
):
let i = 5;
console.log(--i); // Outputs 4
Operator Precedence
Operator precedence determines the order in which operations are performed in an expression.
- Order of Operations:
- Parentheses
()
- Exponentiation
**
- Multiplication
*
, Division/
, Modulus%
- Addition
+
, Subtraction-
- Example:
let result = 10 + 5 * 2; // 20
// Multiplication happens before addition
// 10 + (5 * 2) = 10 + 10 = 20
let resultWithParentheses = (10 + 5) * 2; // 30
// Parentheses alter the order
// (10 + 5) * 2 = 15 * 2 = 30
Type Coercion with the `+` Operator
- When adding a number and a string, JavaScript converts the number to a string and concatenates.
let message = "The total is: " + 20; // "The total is: 20"
- Be cautious to avoid unexpected results.
console.log(5 + "5"); // "55"
console.log(5 - "2"); // 3 (string "2" is coerced to number)
Unary Operators
- Unary Plus (
+
):
- Attempts to convert the operand to a number.
- Example:
let x = "5";
let num = +x; // 5 (number)
- Unary Negation (
-
):
- Converts the operand to a number and negates it.
- Example:
let x = "5";
let num = -x; // -5 (number)
Examples of Arithmetic Operations
- Calculating the Area of a Circle:
const PI = 3.1416;
let radius = 5;
let area = PI * radius ** 2;
console.log("Area:", area); // Area: 78.54
- Even or Odd Checker:
let number = 7;
if (number % 2 === 0) {
console.log(number + " is even.");
} else {
console.log(number + " is odd."); // Outputs: 7 is odd.
}
- Incrementing Values in a Loop:
for (let i = 0; i < 5; i++) {
console.log("Count:", i);
}
// Outputs:
// Count: 0
// Count: 1
// Count: 2
// Count: 3
// Count: 4
Common Mistakes to Avoid
- Integer Division:
- Unlike some languages, dividing integers in JavaScript returns a floating-point number.
console.log(5 / 2); // 2.5
- Modulus with Negative Numbers:
- Be cautious, as the result can be negative.
console.log(-10 % 3); // -1
Exercise
- Simple Calculator:
- Create variables
a
andb
with values12
and4
. - Perform all arithmetic operations and log the results.
let a = 12;
let b = 4;
console.log("Addition:", a + b); // 16
console.log("Subtraction:", a - b); // 8
console.log("Multiplication:", a * b); // 48
console.log("Division:", a / b); // 3
console.log("Modulus:", a % b); // 0
- Increment and Decrement Practice:
- Initialize
counter
to10
. - Use both prefix and postfix increment/decrement operators and observe the outputs.
let counter = 10;
console.log(counter++); // 10
console.log(counter); // 11
console.log(++counter); // 12
console.log(counter--); // 12
console.log(counter); // 11
console.log(--counter); // 10
Comments
Please log in to leave a comment.