Loading...

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
2 lines
|
15/ 500 tokens
1
2
let result = 120 + 80;
console.log(result); // Output: 200
Code Tools

Example: Using Variables

javascript
4 lines
|
19/ 500 tokens
1
2
3
4
let a = 15;
let b = 10;
let total = a + b;
console.log(total); // Output: 25
Code Tools

Example: Using Expressions

javascript
3 lines
|
25/ 500 tokens
1
2
3
let multiplier = 4;
let totalCost = (20 + 30) * multiplier;
console.log(totalCost); // Output: 200
Code Tools

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 lines
|
16/ 500 tokens
1
let sum = 10 + 5; // 10 and 5 are operands, + is the operator
Code Tools

Common Arithmetic Operations

1. Addition (+)

Adds two numbers or concatenates strings.

Example:

javascript
4 lines
|
27/ 500 tokens
1
2
3
4
let apples = 7;
let oranges = 3;
let totalFruits = apples + oranges;
console.log(totalFruits); // Output: 10
Code Tools

2. Subtraction (

Subtracts one number from another.

Example:

javascript
4 lines
|
31/ 500 tokens
1
2
3
4
let initialStock = 50;
let sold = 15;
let remainingStock = initialStock - sold;
console.log(remainingStock); // Output: 35
Code Tools

3. Multiplication (*)

Multiplies two numbers.

Example:

javascript
4 lines
|
31/ 500 tokens
1
2
3
4
let pricePerItem = 20;
let quantity = 3;
let totalPrice = pricePerItem * quantity;
console.log(totalPrice); // Output: 60
Code Tools

4. Division (/)

Divides one number by another.

Example:

javascript
4 lines
|
35/ 500 tokens
1
2
3
4
let totalMarks = 450;
let numberOfSubjects = 5;
let averageMarks = totalMarks / numberOfSubjects;
console.log(averageMarks); // Output: 90
Code Tools

5. Modulus (%)

Returns the remainder of a division.

Example:

javascript
4 lines
|
36/ 500 tokens
1
2
3
4
let totalCookies = 22;
let cookiesPerBox = 6;
let remainingCookies = totalCookies % cookiesPerBox;
console.log(remainingCookies); // Output: 4
Code Tools

6. Increment (++)

Increases a number by one.

Example:

javascript
3 lines
|
16/ 500 tokens
1
2
3
let counter = 0;
counter++;
console.log(counter); // Output: 1
Code Tools

7. Decrement (--)

Decreases a number by one.

Example:

javascript
3 lines
|
18/ 500 tokens
1
2
3
let countdown = 10;
countdown--;
console.log(countdown); // Output: 9
Code Tools

8. Exponentiation (**)

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

Example:

javascript
4 lines
|
31/ 500 tokens
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
Code Tools

Operator Precedence

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

Example:

javascript
2 lines
|
16/ 500 tokens
1
2
let result = 100 + 50 * 2;
console.log(result); // Output: 200
Code Tools

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
2 lines
|
16/ 500 tokens
1
2
let result = (100 + 50) * 2;
console.log(result); // Output: 300
Code Tools

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
2 lines
|
16/ 500 tokens
1
2
let result = 100 - 20 + 5;
console.log(result); // Output: 85
Code Tools

Frequently Asked Questions

JavaScript arithmetic operators perform mathematical operations like addition, subtraction, multiplication, division, and modulus on numeric values.

An arithmetic operator is a symbol used to perform basic math operations, such as + for addition, - for subtraction, * for multiplication, and / for division.

When 3 + '3' is executed, JavaScript converts the number into a string, resulting in '33' instead of 6, due to type coercion.

JavaScript follows the BODMAS rule (Brackets, Orders, Division/Multiplication, Addition/Subtraction) to determine the order of arithmetic operations.

An arithmetic expression in JavaScript is any mathematical computation using numbers and arithmetic operators, such as 5 + 3 * 2, which evaluates to 11.

Still have questions?Contact our support team