web analytics

JavaScript Arithmetic

JavaScript arithmetic is the process of performing mathematical calculations on numbers using JavaScript operators. JavaScript provides a variety of arithmetic operators, including the following:

Addition (+): Adds two numbers together.

Subtraction (-): Subtracts the right number from the left number.

Multiplication (*): Multiplies two numbers together.

Division (/): Divides the left number by the right number.

Modulus (%): Returns the remainder of dividing the left number by the right number.

Exponentiation (**): Raises the left number to the power of the right number.

Increment (++): Increments a number by one.

Decrement (--): Decrements a number by one.

Arithmetic Expressions

An arithmetic expression is a combination of operands (numbers or variables) and operators that evaluates to a single value. For example, the following are all valid arithmetic expressions in JavaScript:

5 + 2
10 - 3
4 * 5
20 / 2
10 % 3
2 ** 3
x++
y--

Arithmetic expressions are evaluated in order of precedence, which means that certain operators are evaluated before others. For example, multiplication and division are evaluated before addition and subtraction. If you need to change the order of evaluation, you can use parentheses.

For example, the following expression evaluates to 16:

10 + 2 * 3

This is because the multiplication is evaluated before the addition. If you want the addition to be evaluated first, you can use parentheses:

(10 + 2) * 3

This expression evaluates to 18.

Arithmetic Operators

Here is a more detailed description of each arithmetic operator:

Addition (+): The addition operator adds two numbers together. The result of the addition is a new number that is the sum of the two operands.

Subtraction (-): The subtraction operator subtracts the right number from the left number. The result of the subtraction is a new number that is the difference between the two operands.

Multiplication (*): The multiplication operator multiplies two numbers together. The result of the multiplication is a new number that is the product of the two operands.

Division (/): The division operator divides the left number by the right number. The result of the division is a new number that is the quotient of the two operands.

Modulus (%): The modulus operator returns the remainder of dividing the left number by the right number. The result of the modulus operation is a new number that is the remainder of the division.

Exponentiation (**): The exponentiation operator raises the left number to the power of the right number. The result of the exponentiation operation is a new number that is the base number raised to the power of the exponent.

Increment (++): The increment operator increments a number by one. The increment operator can be used either before or after the operand. If the increment operator is used before the operand, the operand is incremented and then evaluated. If the increment operator is used after the operand, the operand is evaluated and then incremented.

Decrement (--): The decrement operator decrements a number by one. The decrement operator can be used either before or after the operand, just like the increment operator.

Examples

Here are some examples of how to use JavaScript arithmetic operators:

// Addition
const sum = 10 + 5; // sum is now equal to 15

// Subtraction
const difference = 10 - 5; // difference is now equal to 5

// Multiplication
const product = 10 * 5; // product is now equal to 50

// Division
const quotient = 10 / 5; // quotient is now equal to 2

// Modulus
const remainder = 10 % 5; // remainder is now equal to 0

// Exponentiation
const power = 2 ** 3; // power is now equal to 8

// Increment
let count = 0;
count++;
// count is now equal to 1

// Decrement
let count = 10;
count--;
// count is now equal to 9

Conclusion

JavaScript arithmetic is a powerful tool that can be used to perform a variety of mathematical calculations. By understanding how to use JavaScript arithmetic operators, you can write JavaScript code that is both efficient and expressive.