Arithmetic Operators
JavaScript supports the standard mathematical operations.
| Operator | Action | Example | Result |
|---|---|---|---|
| + | Addition / Concat | 10 + 5 | 15 |
| - | Subtraction | 10 - 5 | 5 |
| * | Multiplication | 10 * 5 | 50 |
| / | Division | 10 / 4 | 2.5 |
| % | Remainder | 10 % 3 | 1 |
| ** | Exponentiation | 2 ** 3 | 8 |
javascript
Comparison Operators: == vs ===
This is one of the most important concepts in JavaScript.
==(Loose Equality): Checks value only. It tries to convert types to match (type coercion).===(Strict Equality): Checks both value and type.
javascript
Best Practice: Always use===and!==. Loose equality leads to unpredictable bugs.
Logical Operators
| Operator | Name | Logic |
|---|---|---|
| && | AND | Both must be true |
| || | OR | At least one must be true |
| ! | NOT | Invert the boolean |
javascript
Nullish Coalescing (??)
Introduced in ES2020, this operator returns the right-hand value only if the left-hand value is
null or undefined. This is safer than using ||, which also triggers for 0 or "".javascript
The Ternary Operator (?:)
A concise one-line
if/else statement.javascript
Summary
| Operator | Usage | Purpose |
|---|---|---|
| === | a === b | Strict equality (Recommended!) |
| == | a == b | Loose equality (Avoid!) |
| && / || | a && b | Logical operations |
| ?? | a ?? b | Handle null/undefined safely |
| ! | !status | Boolean inversion |
| typeof | typeof x | Get data type |
| delete | delete obj.x | Remove object property |