This article will explain and list JavaScript operators and expressions. JavaScript operators perform a given operation on the provided expressions or values. Common operations include comparisons, arithmetic, and ternary operations.
You will use JavaScript operators frequently (even the simplest JavaScript code will use multiple operators!), so it’s worth getting to know them, and their expected behaviour.
What are Operators and Operands?
Javascript operators perform a given operation, for example addition, subtraction, and other arithmetic, comparisons like greater than/less than, and more complicated operations like ternary/comparison operations.
The values, variables, or expressions that operators interact with are called operands. There are usually two operands – one either side of the operator, but in some cases there is only 1.
For example, below the values 1
and 2
are the two operands for the +
operator:
1 + 2
The NOT operator (`!`) only requires a single operand (in this case `0`):
!0
In short, JavaScript operators perform operations on their adjacent operands.
What are Expressions?
An expression is a piece of code that results in a value, for example:
1 + 2
Is a valid JavaScript expression that results in the number 3
.
Single numbers, strings, and boolean values are also valid expressions, so:
4
…is also a valid expression.
Expressions can be as simple or as complicated as a situation requires:
(1 + 3) * (2 / 3)
Is a more complicated expression that uses brackets to set the precedence of evaluation.
Comparison Operators
Comparison operators compare two values or variables (operands). All of the below operators will return TRUE if the expression is correct and FALSE if it is not.
Operator | Meaning | Example |
---|---|---|
== |
The values to the left and right of the operator are equal | myVariable == 4 |
!= |
The values to the left and right of the operator are not equal | myVariable != 5 |
=== (Strict equal) |
The values to the left and right of the operator are equal in value and of the same type | myVariable === 4 |
!== (Strict not equal) |
The values to the left and right of the operator differ in either value or type | myVariable !== "4" |
> |
The left operand is greater than right | myVariable > 4 |
>= |
The left operand is greater than or equal to the right | myVariable >= 4 |
< |
The left operand is less than the right | myVariable < 4 |
<= |
The left operand is less than or equal to the right | myVariable <= 4 |
Logical Operators
Logical operators allow you to chain logical operations.
Operator | Example | Meaning |
---|---|---|
&& (AND) |
expression1 && expression2 |
Returns either expression1 if it evaluates to FALSE, or expression2 . |
|| (OR) |
expression1 || expression2 |
Returns either expression1 if it evaluates to TRUE, or expression2 |
! (NOT) |
!expression |
Returns FALSE if expression is evaluates to TRUE, otherwise returns FALSE (effectively inverting the boolean value) |
Assignment Operators
Assignment operators set the value of a variable.
Operator | Usage | Longhand / meaning |
---|---|---|
= (Assignment) |
myVariable = expression |
myVariable = expression |
+= (Addition assignment) |
myVariable += expression |
myVariable = myVariable + expression |
-= (Subtraction assignment) |
myVariable -= expression |
myVariable = myVariable - expression |
*= (Multiplication assignment) |
myVariable *= expression |
myVariable = myVariable * expression |
/= (Division assignment) |
myVariable /= expression |
myVariable = myVariable / expression |
%= (Remainder assignment) |
myVariable %= expression |
myVariable = myVariable % expression |
**= (Exponentiation assignment) |
myVariable **= expression |
myVariable = myVariable ** expression |
<<= (Left shift assignment) |
myVariable <<= expression |
myVariable = myVariable << expression |
>>= (Right shift assignment) |
myVariable >>= expression |
myVariable = myVariable >> expression |
>>>= (Unsigned right shift assignment) |
myVariable >>>= expression |
myVariable = myVariable >>> expression |
&= (Bitwise AND assignment) |
myVariable &= expression |
myVariable = myVariable & expression |
^= (Bitwise XOR assignment) |
myVariable ^= expression |
myVariable = myVariable ^ expression |
|= (Bitwise OR assignment) |
myVariable |= expression |
myVariable = myVariable | expression |
&&= (Logical AND assignment) |
myVariable &&= expression |
myVariable && (myVariable = expression) |
||= (Logical OR assignment) |
myVariable ||= expression |
myVariable || (myVariable = expression) |
??= (Logical nullish assignment) |
myVariable ??= expression |
myVariable ?? (myVariable = expression) |
Arithmetic Operators
Arithmetic operators perform mathematical operations such as addition, subtraction, multiplication, and division.
Operator | Meaning | Example |
---|---|---|
% (Remainder) |
Returns the remainder from dividing the two operands. | 13 % 5 returns 3 |
++ (Increment) |
Adds one to the operand | myVariable++ will return the value of myVariable and then add 1 to the value of myVariable (postfix increment), ++myVariable will increment the value of myVariable and return the incremented value (prefix increment) |
-- (Decrement) |
Subtracts one from the operand | myVariable-- will return the value of myVariable and then subtract 1 from the value of myVariable (postfix decrement), --myVariable will decrement the value of myVariable and return the decremented value (prefix decrement) |
- (Unary negation) |
Returns the negation of its operand. | If x is 3, then -x returns -3 |
+ (Unary plus) |
Converts the operand to a number, if it can be coerced. | +"4" will convert the a string containing the 4 character to a number |
** (Exponentiation operator) |
The first operand is treated as the base, and the second as the exponent | 2 ** 4 returns 16 |
BigInt Operators
Any operators that can be used with regular number variables can also be used with BigInt variables, with the exception of the unsigned right shift operator, which does not work with BigInt values.
Conditional/Ternary Operators
The conditional or ternary operator will return one of two values based on a condition. The ternary operator is unique in that it takes three operands.
condition ? value1 : value2
If the condition is true, the operator will return the first value (before the :
), whereas if it is false, the second value will be returned.
Type Operators
The type of a variable can be found by using the typeof
operator, for example:
typeof 1
… will return number
as the value passed to it is numerical.
To check whether an object is a specific type, you can use the instanceof
operator:
[] instanceof Array
Above, an empty array is initialized using square bracket notation, and it’s type is confirmed to be an Array using instanceof
.
Comma Operator
The comma operator returns the value of its last operand. This is usually used in for loops so that multiple variables can be updated in each iteration. For example:
for (let i = 0, j = 0; j <= 50; i++, j = j + 2) { console.log('i is ' + i); console.log('j is ' + j); }
Above, the values of both i
and j
are defined and incremented in the loop using the comma operator.
String operators
All comparison operators can be used with strings. Additionally, when dealing with strings, the addition operator (+) aces as the concatenation operator, joining two strings and returning their concatenated value. For example:
'peanut' + ' butter'
Will result in a single string containing the concatenated values:
peanut butter
Operator Precedence
Just like with regular arithmetic, operations in JavaScript have a set precedence, so that the results of a calculation or expression are always the same.
This is otherwise known as the order of operations, and while it largely follows the standards used in mathematics, the additional operators in JavaScript mean that some extra rules are necessary:
It is worth getting to know these rules – you must know what results to expect when manipulating numbers in your code, especially if you are working with financial data or in other scenarios where calculation errors could result in real world consequences.
- As in mathematics, exponents are evaluated before multiplication and division, and multiplication and division take a higher precedence than addition and subtraction
- The operations within parenthesis/brackets (
()
) are evaluated first. Expressions in nested parenthesis will be evaluated before those that contain them - When several operations have the same precedence they will be evaluated in order from left-to-right
- Functions are evaluated before any expressions are evaluated, and their result is used in their place
- Postfix increments and decrements before prefix increments and decrements
- Variable assignments are always performed last, so that the final result of an expression is always assigned to a variable
Grouping Operator
The parenthesis/brackets (()
) used to set the order of operations above are operators themselves, called grouping operators. Grouping operators are used solely for this purpose, returning the value of the expression contained within them.
For more information on JavaScript operators, check out the Mozilla developer network documentation.