📘 JavaScript Operators
In JavaScript, operators are special symbols used to perform operations on values and variables.
For example:
1. Arithmetic Operators
Used to perform basic mathematical operations.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 2 | 7 |
- | Subtraction | 5 - 2 | 3 |
* | Multiplication | 5 * 2 | 10 |
/ | Division | 10 / 2 | 5 |
% | Modulus (remainder) | 5 % 2 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
++ | Increment | let x=5; x++ → 6 | Increases by 1 |
-- | Decrement | let x=5; x-- → 4 | Decreases by 1 |
2. Assignment Operators
Used to assign values to variables.
| Operator | Example | Same as |
|---|---|---|
= | x = 10 | x = 10 |
+= | x += 5 | x = x + 5 |
-= | x -= 5 | x = x - 5 |
*= | x *= 5 | x = x * 5 |
/= | x /= 5 | x = x / 5 |
%= | x %= 5 | x = x % 5 |
**= | x **= 2 | x = x ** 2 |
3. Comparison Operators
Used to compare two values. They return true or false.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to (value only) | 5 == "5" | true |
=== | Equal value & type | 5 === "5" | false |
!= | Not equal (value only) | 5 != "5" | false |
!== | Not equal (value + type) | 5 !== "5" | true |
> | Greater than | 10 > 5 | true |
< | Less than | 10 < 5 | false |
>= | Greater than or equal | 10 >= 10 | true |
<= | Less than or equal | 5 <= 10 | true |
4. Logical Operators
Used to combine conditions.
| Operator | Meaning | Example | Result |
|---|---|---|---|
&& | AND | (5 > 3 && 10 > 5) | true |
|| | OR | (5 > 3 || 10 > 11) | true |
! | NOT | !(5 > 3) | false |
5. Bitwise Operators
Operate on binary numbers (rarely used for beginners, but important).
| Operator | Name | 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 |
6. String Operators
The + operator can also concatenate strings.
📌 += can also be used with strings:
7. Type Operators
| Operator | Usage |
|---|---|
typeof | Returns the type of a variable → typeof 123 → "number" |
instanceof | Checks if an object is an instance of a class → arr instanceof Array → true |
8. Ternary Operator (?:)
Shortcut for an if-else statement.
🔑 Summary
-
Arithmetic → Math operations
-
Assignment → Assign values
-
Comparison → Compare values (true/false)
-
Logical → Combine conditions
-
Bitwise → Binary operations
-
String → Concatenation
-
Type → Type checking
-
Ternary → Short
if-else
Comments
Post a Comment