JavaScript Operators
Operators are the building blocks of any JavaScript program. They let you do math, compare values, make decisions, and store data. If you have ever written 2 + 2 or asked "is this value equal to that one?", you have already used an operator. Let's break them all down simply.
What are Operators?
An operator is a symbol that performs an action on one or more values. The values it works on are called operands.
5 + 3 // here, + is the operator, 5 and 3 are the operands
1. Arithmetic Operators
These do basic math. Nothing surprising here.
| Operator | What it does | Example | Result |
|---|---|---|---|
+ |
Addition | 10 + 3 |
13 |
- |
Subtraction | 10 - 3 |
7 |
* |
Multiplication | 10 * 3 |
30 |
/ |
Division | 10 / 2 |
5 |
% |
Remainder (Modulo) | 10 % 3 |
1 |
The % operator is worth a special mention. It gives you what is left over after division. So 10 % 3 is 1 because 3 goes into 10 three times, with 1 remaining. It is great for checking if a number is odd or even.
console.log(10 + 3); // 13
console.log(10 % 3); // 1
console.log(8 % 2); // 0 (perfectly even, no remainder)
2. Comparison Operators
These compare two values and always return either true or false.
| Operator | What it does | Example | Result |
|---|---|---|---|
== |
Equal (loose) | 5 == "5" |
true |
=== |
Equal (strict) | 5 === "5" |
false |
!= |
Not equal | 5 != 3 |
true |
> |
Greater than | 10 > 5 |
true |
< |
Less than | 3 < 7 |
true |
The important one: == vs ===
This trips up a lot of beginners.
==checks if values are equal, but it does not care about the type. So it will convert types before comparing.===checks if values are equal AND the same type. No conversion happens
console.log(5 == "5"); // true — JS converts "5" to a number first
console.log(5 === "5"); // false — number is not the same type as string
console.log(5 === 5); // true — same value, same type
3. Logical Operators
These are used to combine or reverse conditions. They are the backbone of decision making in JavaScript.
| Operator | Name | What it does |
|---|---|---|
&& |
AND | Returns true only if both sides are true |
| ` | ` | |
! |
NOT | Flips true to false, and false to true |
let age = 20;
let hasTicket = true;
console.log(age >= 18 && hasTicket); // true — both conditions met
console.log(age < 18 || hasTicket); // true — at least one is true
console.log(!hasTicket); // false — flips true to false
4. Assignment Operators
These store values into variables. You already know =, but the shorthand versions save you from writing repetitive code.
| Operator | What it does | Same as |
|---|---|---|
= |
Assigns a value | x = 5 |
+= |
Adds and assigns | x = x + 3 |
-= |
Subtracts and assigns | x = x - 3 |
let score = 10;
score += 5; // score is now 15
score -= 3; // score is now 12
console.log(score); // 12
These are just shortcuts. score += 5 means exactly the same as score = score + 5.
That covers everything you need to know about JavaScript operators as a beginner.