Arithmetic Operators in JavaScript

The JavaScript arithmetic operators take numerical values as their left & right operands, perform the arithmetic operation, and return a numerical value. The JavaScript supports all the arithmetic operators like addition (+), subtraction (-), multiplication (*), division (/), etc. Note that all the numbers in JavaScript are represented as IEEE 754 floating-point numbers and use floating-point arithmetic.

Arithmetic Operators

The following are the List of Arithmetic Operators in JavaScript

OperatorDescription
+Addition
Subtraction
*Multiplication
**Exponentiation
/Division
%Modulus (Remainder)
++Increment
Decrement
+Unary plus
Unary minus

Addition (+)

The addition operator (+) is a binary operator, which calculates the sum of two numeric operands.

If one of the operands is a string, then the + operator does a string concatenation

The booleans are implemented as numerical values with a single binary digit (i.e., 0 & 1). 1 is true & 0 is false.

Subtraction ()

The subtraction operator subtracts the right operand from the left operand. If any of the operands is not a number, then it returns a NaN

Converts strings to numbers.

Subtraction, when one (or both) of the operand is not a number, always results in NaN

Multiplication (*)

The multiplication operator (*) multiplies the left operand with the right operand.

Strings are converted to numbers.

Infinity

Multiplication with non-numbers results in NaN.

Division (/)

The division operator (/) divides the left operand (dividend) with the right operand (divisor).

Example

Strings are converted to numbers.

If the string is not a number, then the result is NaN.

Booleans are numbers. True is 1 & false is 0

Dividing by 0 results in Infinity

Modulus or Reminder (%)

The remainder operator (%) returns the remainder leftover of a division operation between the operands. The result always takes the sign of the dividend.

Increment (++) & Decrement ()

We use the increment & Decrement operators to increase or decrease the value of the variable by one. JavaScript uses the ++ (increment) & -- (decrement) to denote them. We can either prefix or Postfix these operators.

Increment & Decrement Operators in JavaScript

Unary plus (+) & Unary minus ()

The unary plus operator (+) precedes its operand and converts it into a number. If it fails to convert the operand into a number, then it returns NaN. The unary (-) operator converts the operand into a number and negates it.

Unary plus & Unary minus operators in JavaScript

Exponentiation (**)

The exponentiation operator (**) returns the result of raising the first operand to the power of the second operand.

BigInt & Arithmetic Operators

The BigInt can be used with the following arithmetic operations. Addition (+), Subtraction (-), Multiplication (*), Exponentiation (%), Division (/), Modulus (Remainder) (%), Increment (++), Decrement (–).

Unary plus (+) & Unary minus (-) are not supported

The / division operator rounds of the final result to the whole number. For example, dividing 5/2 results in 2 and not 2.5. i.e it is an integer and not decimal.

Reference

  1. Expressions & Operators
  2. Precedence & Associativity

Leave a Comment

Your email address will not be published.

Scroll to Top