Lessons

JavaScript Basics

Operators in JavaScript

Conditional Statements in JavaScript

JavaScript Strings

JavaScript Arrays

JavaScript Loop

JavaScript Functions

Conclusion

JavaScript Arithmetic Operators

JavaScript Arithmetic

JavaScript arithmetic operators are used to perform mathematical operations on numbers. Whether you're adding, subtracting, or calculating powers, understanding how these operators work will help you manipulate numerical data efficiently.

Arithmetic Operations in JavaScript

Arithmetic operations can be applied to:

  • Literals: Direct numbers like 100 + 50
  • Variables: Using values stored in variables
  • Expressions: Combinations of literals, variables, and other expressions

Example: Using Literals

javascript
1
2
let result = 120 + 80;
console.log(result); // Output: 200

Example: Using Variables

javascript
1
2
3
4
let a = 15;
let b = 10;
let total = a + b;
console.log(total); // Output: 25

Example: Using Expressions

javascript
1
2
3
let multiplier = 4;
let totalCost = (20 + 30) * multiplier;
console.log(totalCost); // Output: 200

Operators and Operands

In arithmetic, operands are the numbers or variables that the operation is performed on, while the operator defines the type of operation.

Example:

javascript
1
let sum = 10 + 5; // 10 and 5 are operands, + is the operator

Common Arithmetic Operations

1. Addition (+)

Adds two numbers or concatenates strings.

Example:

javascript
1
2
3
4
let apples = 7;
let oranges = 3;
let totalFruits = apples + oranges;
console.log(totalFruits); // Output: 10

2. Subtraction (

Subtracts one number from another.

Example:

javascript
1
2
3
4
let initialStock = 50;
let sold = 15;
let remainingStock = initialStock - sold;
console.log(remainingStock); // Output: 35

3. Multiplication (*)

Multiplies two numbers.

Example:

javascript
1
2
3
4
let pricePerItem = 20;
let quantity = 3;
let totalPrice = pricePerItem * quantity;
console.log(totalPrice); // Output: 60

4. Division (/)

Divides one number by another.

Example:

javascript
1
2
3
4
let totalMarks = 450;
let numberOfSubjects = 5;
let averageMarks = totalMarks / numberOfSubjects;
console.log(averageMarks); // Output: 90

5. Modulus (%)

Returns the remainder of a division.

Example:

javascript
1
2
3
4
let totalCookies = 22;
let cookiesPerBox = 6;
let remainingCookies = totalCookies % cookiesPerBox;
console.log(remainingCookies); // Output: 4

6. Increment (++)

Increases a number by one.

Example:

javascript
1
2
3
let counter = 0;
counter++;
console.log(counter); // Output: 1

7. Decrement (--)

Decreases a number by one.

Example:

javascript
1
2
3
let countdown = 10;
countdown--;
console.log(countdown); // Output: 9

8. Exponentiation (**)

Raises the first operand to the power of the second operand.

Example:

javascript
1
2
3
4
let base = 3;
let power = 4;
let result = base ** power; // Equivalent to Math.pow(3, 4)
console.log(result); // Output: 81

Operator Precedence

Operator precedence determines the order in which operations are performed. Higher precedence operators are executed before lower precedence operators.

Example:

javascript
1
2
let result = 100 + 50 * 2;
console.log(result); // Output: 200

Explanation: Multiplication has higher precedence, so 50 * 2 is calculated first, then added to 100.

Changing Precedence with Parentheses

Parentheses can be used to override the default precedence.

Example:

javascript
1
2
let result = (100 + 50) * 2;
console.log(result); // Output: 300

Explanation: The operation inside the parentheses (100 + 50) is calculated first.

Left-to-Right Execution

When multiple operations have the same precedence, they are executed from left to right.

Example:

javascript
1
2
let result = 100 - 20 + 5;
console.log(result); // Output: 85

Frequently Asked Questions