JavaScript operators perform operations on one or more operands and produce a result.
For example, in 1 + 2, the + symbol is an operator and returns the result 3.
JavaScript includes the following categories of operators:
Arithmetic operators perform mathematical calculations.
| Operator | Description |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus (Remainder) |
| ++ | Increment |
| -- | Decrement |
Arithmetic operators perform calculations between numbers.
The operators return addition, subtraction, multiplication, division and remainder values.
The ++ and -- operators increase or decrease a variable by one.
The + operator joins strings together.
If one operand is a string, the + operator performs concatenation.
Comparison operators compare two values and return either true or false.
| Operator | Description |
|---|---|
| == | Equal to |
| === | Strict equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
Comparison operators compare values and return either true or false.
The strict equality operator (===) compares both value and data type.
Logical operators combine multiple conditions and return a boolean result.
| Operator | Description |
|---|---|
| && | Logical AND |
| || | Logical OR |
| ! | Logical NOT |
Logical operators are commonly used with conditions.
Assignment operators assign values to variables and perform calculations.
| Operator | Description |
|---|---|
| = | Assign value |
| += | Add and assign |
| -= | Subtract and assign |
| *= | Multiply and assign |
| /= | Divide and assign |
| %= | Modulus and assign |
Assignment operators reduce the amount of code needed for calculations.
These operators perform the calculation and assign the result back to the variable.
JavaScript follows mathematical precedence rules while evaluating expressions.
Multiplication happens before addition, so the result is 20.
The result becomes 30.
JavaScript provides a special operator called the ternary operator (? :). It is a shorthand version of the if...else statement.
The syntax is:
If the condition is true, the first value is returned; otherwise, the second value is returned.
The variable c stores the larger value, while d stores the smaller value.
Which operator is used for strict equality in JavaScript?