JavaScript operators are symbols or keywords used to perform operations on values and variables. Operators are essential to almost every JavaScript expression.
Used for numeric calculations.
Operator | Description | Example | Result |
---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 10 - 4 |
6 |
* |
Multiplication | 2 * 4 |
8 |
/ |
Division | 8 / 2 |
4 |
% |
Modulus (remainder) | 7 % 3 |
1 |
** |
Exponentiation | 2 ** 3 |
8 |
++ |
Increment | let i = 1; i++ |
2 |
-- |
Decrement | let i = 1; i-- |
0 |
Used to assign values to variables.
Operator | Example | Meaning |
---|---|---|
= |
x = 10 |
Assign 10 to x |
+= |
x += 5 |
x = x + 5 |
-= |
x -= 3 |
x = x - 3 |
*= |
x *= 2 |
x = x * 2 |
/= |
x /= 4 |
x = x / 4 |
%= |
x %= 2 |
x = x % 2 |
**= |
x **= 3 |
x = x ** 3 |
Used to compare values.
Operator | Description | Example | Result |
---|---|---|---|
== |
Equal to (loose) | 5 == '5' |
true |
=== |
Equal to (strict) | 5 === '5' |
false |
!= |
Not equal to | 5 != '5' |
false |
!== |
Strict not equal | 5 !== '5' |
true |
> |
Greater than | 8 > 5 |
true |
< |
Less than | 3 < 4 |
true |
>= |
Greater than or equal | 5 >= 5 |
true |
<= |
Less than or equal | 7 <= 6 |
false |
Used for boolean logic.
Operator | Description | Example | Result | ||||
---|---|---|---|---|---|---|---|
&& |
AND | true && false |
false |
||||
` | | ` | OR | `true | | false` | true |
||||
! |
NOT | !true |
false |
Operate on binary representations.
Operator | Description | Example | ||
---|---|---|---|---|
& |
AND | 5 & 1 => 1 |
||
` | ` | OR | `5 | 1=> 5` |
||
^ |
XOR | 5 ^ 1 => 4 |
||
~ |
NOT | ~5 => -6 |
||
<< |
Left shift | 5 << 1 => 10 |
||
>> |
Right shift | 5 >> 1 => 2 |
Operate on a single operand.
Operator | Description | Example |
---|---|---|
typeof |
Returns type | typeof 42 → "number" |
delete |
Deletes object prop | delete obj.name |
void |
Returns undefined | void(0) → undefined |
! |
Logical NOT | !false → true |
A shorthand for if-else.
let age = 18;
let access = (age >= 18) ? "Allowed" : "Denied";
console.log(access); // Allowed
Used to concatenate strings.
let first = "Code";
let second = "Harbor";
console.log(first + second); // "CodeHarbor"
Check the data type or construct.
Operator | Use Case |
---|---|
typeof |
typeof "hello" → "string" |
instanceof |
arr instanceof Array → true |
Evaluates multiple expressions and returns the last.
let x = (1 + 2, 3 + 4);
console.log(x); // 7
?.
): Safe access to nested properties.let user = {};
console.log(user.profile?.name); // undefined (no error)
??
): Returns right operand if left is null
or undefined
.let value = null ?? "Default";
console.log(value); // "Default"
==
and ===
?&&
and ||
?delete
operator?graph TD
A[Operators] --> B[Arithmetic]
A --> C[Assignment]
A --> D[Comparison]
A --> E[Logical]
A --> F[Bitwise]
A --> G[Unary]
A --> H[Ternary]
A --> I[String]
A --> J[Type]
A --> K[Comma]
A --> L[Optional Chaining / Nullish Coalescing]
This content is crafted by Ajay Dhangar. If you find it helpful, don’t forget to ⭐ star this repo and follow on CodeHarborHub for more such content!