Js Operators in JavaScript
? JavaScript Operators
Operators in JavaScript are special symbols used to perform operations on operands (variables or values). They are the foundation of expressions in JavaScript.
? 1. Arithmetic Operators
Used for mathematical calculations.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 2 | 7 |
- | Subtraction | 5 - 2 | 3 |
* | Multiplication | 5 * 2 | 10 |
/ | Division | 10 / 2 | 5 |
% | Modulus | 5 % 2 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
++ | Increment | x++ | x + 1 |
-- | Decrement | x-- | x - 1 |
? 2. Assignment Operators
Assign values to variables.
| Operator | Example | Same As |
|---|---|---|
= | x = y | x = y |
+= | x += y | x = x + y |
-= | x -= y | x = x - y |
*= | x *= y | x = x * y |
/= | x /= y | x = x / y |
%= | x %= y | x = x % y |
? 3. Comparison Operators
Compare two values and return a Boolean (true or false).
| Operator | Description | Example |
|---|---|---|
== | Equal to | 5 == "5" ? |
=== | Strict equal | 5 === "5" ? |
!= | Not equal | 5 != 3 ? |
!== | Strict not equal | 5 !== "5" ? |
> | Greater than | 5 > 3 ? |
< | Less than | 5 < 3 ? |
>= | Greater or equal | 5 >= 5 ? |
<= | Less or equal | 3 <= 5 ? |
? 4. Logical Operators
Combine multiple conditions.
| Operator | Description | Example |
|---|---|---|
&& | Logical AND | true && false ? false |
| ` | ` | |
! | Logical NOT | !true ? false |
? 5. Type Operators
| Operator | Description | Example |
|---|---|---|
typeof | Returns type | typeof 42 ? "number" |
instanceof | Checks instance | obj instanceof Array |
? 6. Other Useful Operators
Ternary Operator (Conditional):
let age = 18;let result = (age >= 18) ? "Adult" : "Minor";Nullish Coalescing (
??):
Returns right-hand value if left isnullorundefined.let name = null;console.log(name ?? "Guest"); // "Guest"Optional Chaining (
?.):
Avoids errors when accessing deeply nested properties.user?.profile?.email;
? Summary
JavaScript operators are grouped into:
Arithmetic
Assignment
Comparison
Logical
Type
Conditional
They help you perform everything from math to conditionals and value checks.
Would you like a cheat sheet or mini quiz to test your operator knowledge?