JavaScript Operators Overview
JavaScript provides a variety of operators, similar to those found in other programming languages. An operator is a special symbol used to perform operations on one or more operands (data values), producing a result.
For example, in the expression 1 + 2
, the +
symbol is an operator, 1
is the left operand, and 2
is the right operand. The +
operator performs addition on the two numeric values and returns the result.
JavaScript operators are grouped into the following categories:
-
Arithmetic Operators – Perform mathematical operations like addition, subtraction, multiplication, etc.
-
Comparison Operators – Compare two values and return a Boolean result.
-
Logical Operators – Combine or invert Boolean values.
-
Assignment Operators – Assign values to variables.
-
Conditional Operators – Perform operations based on a condition.
-
Ternary Operator – A shorthand for the conditional statement (
condition ? trueResult : falseResult
).
Arithmetic Operators
Arithmetic operators do math on numbers.
Operator | Description | Example | Result |
---|---|---|---|
+ | Adds two numbers | 5 + 2 | 7 |
- | Subtracts right number from left | 10 - 3 | 7 |
* | Multiplies two numbers | 4 * 2 | 8 |
/ | Divides left number by right | 10 / 2 | 5 |
% | Returns the remainder | 7 % 2 | 1 |
++ | Increases a value by 1 | x++ or ++x | x = x + 1 |
-- | Decreases a value by 1 | x-- or --x | x = x - 1 |
Example
Pre-Increment vs Post-Increment
String Concatenation with +
Operator
If you use +
with a string, it joins (concatenates) values instead of adding them.
Example
Comparison Operators
These operators compare two values and return true
or false
.
Operator | Description |
---|---|
== | Equal (ignores type) |
=== | Equal (checks type too) |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
Example
Logical Operators
Used to check multiple conditions.
Operator | Name | Description |
---|---|---|
&& | AND | Returns true if both conditions are true |
` | ` | |
! | NOT | Reverses the condition |
Example
Assignment Operators
These assign or update variable values.
Operator | Description |
---|---|
= | Assign value |
+= | Add and assign |
-= | Subtract and assign |
*= | Multiply and assign |
/= | Divide and assign |
%= | Modulus and assign |
Example
Ternary Operator (?:
)
A shortcut for if...else
statements.