Skip to main content

Archive

Show more

JavaScript Arithmetic Operators

JavaScript Arithmetic Operators

Arithmetic operators in JavaScript are used to perform basic mathematical operations on numbers. They are fundamental to many programming tasks and are essential for performing calculations and manipulating numeric data. This article explores the various arithmetic operators available in JavaScript and provides examples of their usage.


Types of Arithmetic Operators

JavaScript supports several arithmetic operators that perform operations such as addition, subtraction, multiplication, division, and more. Here’s a breakdown of these operators:

1. Addition (+)

The addition operator adds two numbers together. If one or both of the operands are strings, the addition operator will concatenate them.

let a = 10;
let b = 5;

console.log(a + b); // Addition of numbers: 15
console.log('Hello' + ' World'); // Concatenation of strings: Hello World

2. Subtraction (-)

The subtraction operator subtracts one number from another. It cannot be used to concatenate strings.

let a = 10;
let b = 5;

console.log(a - b); // Subtraction: 5

3. Multiplication (*)

The multiplication operator multiplies two numbers together. It cannot be used for string concatenation.

let a = 10;
let b = 5;

console.log(a * b); // Multiplication: 50

4. Division (/)

The division operator divides one number by another. If the divisor is zero, the result is Infinity or -Infinity, depending on the sign of the dividend.

let a = 10;
let b = 5;

console.log(a / b); // Division: 2

let c = 0;
console.log(a / c); // Division by zero: Infinity

5. Modulus (%)

The modulus operator returns the remainder of the division of one number by another. It is useful for determining if a number is divisible by another.

let a = 10;
let b = 3;

console.log(a % b); // Modulus: 1 (remainder of 10 divided by 3)

6. Exponentiation (**)

The exponentiation operator raises the first operand to the power of the second operand.

let a = 2;
let b = 3;

console.log(a ** b); // Exponentiation: 8 (2 raised to the power of 3)

Combining Arithmetic Operators

You can combine arithmetic operators to perform more complex calculations:

let a = 10;
let b = 5;
let c = 2;

let result = (a + b) * c; // Combined operation: (10 + 5) * 2 = 30
console.log(result); // Output: 30

Operator Precedence

JavaScript operators have a defined precedence, which determines the order in which operations are performed. Arithmetic operators have high precedence compared to comparison and logical operators. For example, in the expression 3 + 4 * 2, the multiplication is performed before the addition, resulting in 11.

let result = 3 + 4 * 2; // Multiplication is performed before addition
console.log(result); // Output: 11

Conclusion

Arithmetic operators in JavaScript are essential for performing basic mathematical operations and handling numeric data. By understanding how to use these operators effectively, you can perform a wide range of calculations and build more complex functionality in your JavaScript programs. Mastering arithmetic operators is a foundational skill for any JavaScript developer.

Comments