• 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
1let result = 120 + 80;
2console.log(result); // Output: 200

Example: Using Variables

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

Example: Using Expressions

javascript
1let multiplier = 4;
2let totalCost = (20 + 30) * multiplier;
3console.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
1let sum = 10 + 5; // 10 and 5 are operands, + is the operator

Common Arithmetic Operations

1. Addition (+)

Adds two numbers or concatenates strings.

Example:

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

2. Subtraction (

Subtracts one number from another.

Example:

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

3. Multiplication (*)

Multiplies two numbers.

Example:

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

4. Division (/)

Divides one number by another.

Example:

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

5. Modulus (%)

Returns the remainder of a division.

Example:

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

6. Increment (++)

Increases a number by one.

Example:

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

7. Decrement (--)

Decreases a number by one.

Example:

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

8. Exponentiation (**)

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

Example:

javascript
1let base = 3;
2let power = 4;
3let result = base ** power; // Equivalent to Math.pow(3, 4)
4console.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
1let result = 100 + 50 * 2;
2console.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
1let result = (100 + 50) * 2;
2console.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
1let result = 100 - 20 + 5;
2console.log(result); // Output: 85

Frequently Asked Questions