JavaScript operators are symbols that allow you to perform actions on values and variables.
You use operators to calculate numbers, compare values, assign data, and more.
let x = 10 + 5;
Here + is an operator.
JavaScript has several types of operators:
Let’s learn each one with examples.
Used to perform mathematical calculations.
| Operator | Description | Example (x = 10, y = 5) | Result |
|---|---|---|---|
| + | Addition | x + y |
15 |
| - | Subtraction | x - y |
5 |
| * | Multiplication | x * y |
50 |
| / | Division | x / y |
2 |
| % | Modulus (remainder) | x % y |
0 |
| ** | Exponentiation | x ** 2 |
100 |
| ++ | Increment | x++ |
10 → 11 |
| -- | Decrement | x-- |
10 → 9 |
let a = 7;
let b = 3;
console.log(a % b); // 1
Used to assign values to variables.
| Operator | Meaning | Example | Equivalent to |
|---|---|---|---|
| = | Assign | x = 5 |
— |
| += | Add & assign | x += 3 |
x = x + 3 |
| -= | Subtract & assign | x -= 2 |
x = x - 2 |
| *= | Multiply & assign | x *= 4 |
x = x * 4 |
| /= | Divide & assign | x /= 5 |
x = x / 5 |
| %= | Modulus & assign | x %= 2 |
x = x % 2 |
let x = 10;
x += 5;
console.log(x); // 15
Used to compare values. They return true or false.
| Operator | Description | Example | Result |
|---|---|---|---|
| == | Equal (value only) | 5 == "5" |
true |
| === | Strict equal (value + type) | 5 === "5" |
false |
| != | Not equal | 5 != 3 |
true |
| !== | Strict not equal | 5 !== "5" |
true |
| > | Greater than | 10 > 5 |
true |
| < | Less than | 3 < 1 |
false |
| >= | Greater or equal | 10 >= 10 |
true |
| <= | Less or equal | 5 <= 4 |
false |
👉 Always prefer === instead of == to avoid unexpected type conversion.
Used to combine multiple conditions.
| Operator | Name | Example | Result |
|---|---|---|---|
| && | AND | age >= 18 && hasID |
true (both conditions are true) |
| || | OR | age >= 18 || hasID |
true (at least one condition is true) |
| ! | NOT | !hasID |
false (opposite of true) |
let age = 20;
let hasID = true;
if (age >= 18 && hasID) {
console.log("Access granted");
}
Only one main operator: + (plus) for string concatenation.
let first = "Learn2";
let second = "Kode";
console.log(first + second); // Learn2Kode
let text = "Hello";
text += " World";
console.log(text); // Hello World
Operators that work with one operand.
| Operator | Use | Example |
|---|---|---|
typeof |
Check data type | typeof 10 → "number" |
! |
NOT | !true → false |
+ |
Convert to number | +"10" → 10 |
- |
Negative | -5 → -5 |
Shortcut for if...else.
condition ? valueIfTrue : valueIfFalse
let age = 18;
let message = (age >= 18) ? "Adult" : "Minor";
console.log(message);
| Type | Purpose |
|---|---|
| Arithmetic | Math calculations |
| Assignment | Update/assign values |
| Comparison | Compare values |
| Logical | Combine conditions |
| String | Combine text |
| Unary | Work with one value |
| Ternary | Short if-else |