- Arithmetic Operators
- Comparison Operators
- Logical (or Relational) Operators
- Assignment Operators
- Conditional (or ternary) Operators
The addition operator takes two numerical operands and gives their numerical sum. It also concatenates two strings or numbers.
a + b
// Number + Number => Addition 1 + 2 gives 3 // Number + String => Concatenation 5 + "hello" gives "5Hello"
The subtraction operator gives the difference of two operands in the form of numerical value.
a - b
// Number - Number => Subtraction 10 - 7 gives 3 "Hello" - 1 gives Nan
The multiplication operator gives the product of operands where one operand is multiplicand and another is multiplier.
a * b
// Number * Number => Multiplication 3 * 3 gives 9 -4 * 4 gives -16 Infinity * 0 gives NaN Infinity * Infinity gives Infinity 'hi' * 2 gives NaN
The division operator provides the quotient of its operands where the right operand is the divisor and the left operand is the dividend.
a / b
// Number / Number => Division 5 / 2 gives 2.5 1.0 / 2.0 gives 0.5 3.0 / 0 gives Infinity 4.0 / 0.0 gives Infinity, because 0.0 == 0 2.0 / -0.0 gives -Infinity
The modulus operator returns the remainder left over when a dividend is divided by a divisor. The modulus operator is also known as remainder operator. It takes the sign of the dividend.
a % b
// Number % Number => Modulus of the number 9 % 5 gives 4 -12 % 5 gives -2 1 % -2 gives 1 5.5 % 2 gives 1.5 -4 % 2 gives -0 NaN % 2 gives NaN
The exponentiation operator gives the result of raising the first operand to the power of the second operand. The exponentiation operator is right-associative.
a ** b
// Number ** Number => Exponenial of the number -4 ** 2 // This is an incorrect expression -(4 ** 2) gives -16, this is a correct expression 2 ** 5 gives 32 3 ** 3 gives 27 3 ** 2.5 gives 15.588457268119896 10 ** -2 gives 0.01 2 ** 3 ** 2 gives 512 NaN ** 2 gives NaN
The increment operator increments (adds one to) its operand and returns a value.
- If used postfix with operator after operand (for example, x++), then it increments and returns the value before incrementing.
- If used prefix with operator before operand (for example, ++x), then it increments and returns the value after incrementing.
a++ or ++a
// Postfix var a = 2; b = x++; // b = 2, a = 3 // Prefix var x = 5; y = ++x; // x = 6, y = 6
The decrement operator decrements (subtracts one from) its operand and returns a value.
- If used postfix, with operator after operand (for example, x–), then it decrements and returns the value before decrementing.
- If used prefix, with operator before operand (for example, –x), then it decrements and returns the value after decrementing.
a-- or --a
// Prefix var x = 2; y = --x; gives x = 1, y = 1 // Postfix var x = 3; y = x--; gives y = 3, x = 2
This is a unary operator i.e. it operates on a single operand. It gives the negation of an operand.
var a = 3; b = -a; gives b = -3, a = 3 // Unary negation operator // can convert non-numbers // into a number var a = "3"; b = -a; gives b = -3
This is a way to convert a non-number into a number. Although unary negation (-) also can convert non-numbers, unary plus is the fastest and preferred way of converting something into a number, because it does not perform any other operations on the number.
+4 gives 4 +'2' gives 2 +true gives 1 +false gives 0 +null gives 0