JavaScript Operators: The Basics You Need to Know

By Saurabh Prajapati | IBM Software Engineer & Full-Stack Developer
Why I Decided to Explore This
When I first started learning JavaScript, I thought operators were just... math symbols. Like, how complicated can + and - really be?
Turns out — more interesting than I expected. 😄
The moment that really got me was when I wrote this:
console.log(0 == "0"); // true
console.log(0 === "0"); // false
Wait, what? How are those different?
That confusion sent me down a rabbit hole. And honestly? I'm glad it did. Because once operators clicked for me, writing conditions, doing calculations, and controlling logic in JavaScript became so much easier.
Let me walk you through what I learned — step by step.
So... What Even Is an Operator?
Think of an operator as a symbol that tells JavaScript to do something with values.
That's it.
let result = 5 + 3; // '+' is the operator. It adds 5 and 3.
The values around the operator (like 5 and 3) are called operands. The operator sits between them and does the work.
JavaScript has several types of operators — each with its own job. Let's go through them one by one.
1. Arithmetic Operators — The Math Ones
These are the ones you probably already know. They handle basic math.
| Operator | What It Does | Example | Result |
|---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 5 - 3 |
2 |
* |
Multiplication | 5 * 3 |
15 |
/ |
Division | 6 / 2 |
3 |
% |
Remainder (Modulus) | 7 % 3 |
1 |
Most of these feel natural. But % (modulus) confused me at first.
Here's how I think about it now: it gives you the leftover after division.
console.log(7 % 3); // 1
// Because 7 ÷ 3 = 2, with 1 left over
A super common real-world use? Checking if a number is even or odd:
let num = 8;
console.log(num % 2); // 0 → even!
let num2 = 7;
console.log(num2 % 2); // 1 → odd!
I use this trick all the time. Once I learned it, I started seeing it everywhere in code.
2. Comparison Operators — Is This True or False?
These operators compare two values and return either true or false.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== |
Equal (loose) | 5 == "5" |
true |
=== |
Equal (strict) | 5 === "5" |
false |
!= |
Not equal (loose) | 5 != 3 |
true |
> |
Greater than | 10 > 5 |
true |
< |
Less than | 3 < 7 |
true |
>= |
Greater than or equal | 5 >= 5 |
true |
<= |
Less than or equal | 4 <= 3 |
false |
The Big One: == vs ===
This is where I made my first mistake. And honestly, so does almost everyone.
== (loose equality) compares values, but ignores the data type. JavaScript does some behind-the-scenes type conversion to make the comparison work.
=== (strict equality) compares both the value AND the type. No shortcuts. No conversions.
console.log(5 == "5"); // true → value is same, type is ignored
console.log(5 === "5"); // false → number vs string, not the same!
console.log(0 == false); // true → 0 and false are treated as equal
console.log(0 === false); // false → different types!
My rule now? Always use === unless you have a very specific reason not to.
It avoids bugs that are really hard to track down. Trust me on this one — I've been there.
3. Logical Operators — Combining Conditions
Once you can compare values, you'll want to combine those comparisons. That's where logical operators come in.
There are three of them:
| Operator | Name | Meaning |
|---|---|---|
&& |
AND | Both conditions must be true |
| ` | ` | |
! |
NOT | Flips true to false, false to true |
&& — AND
let age = 20;
let hasID = true;
if (age >= 18 && hasID) {
console.log("You can enter!"); // Both are true → runs this
}
Both conditions have to be true for && to return true.
|| — OR
let isWeekend = false;
let isHoliday = true;
if (isWeekend || isHoliday) {
console.log("No work today! 🎉"); // At least one is true → runs this
}
At least one condition being true is enough for ||.
! — NOT
This one just flips the value.
let isLoggedIn = false;
if (!isLoggedIn) {
console.log("Please log in first."); // !false = true → runs this
}
Truth Table (For Reference)
| A | B | A && B | A || B | !A | | --- | --- | --- | --- | --- | | true | true | true | true | false | | true | false | false | true | false | | false | true | false | true | true | | false | false | false | false | true |
When I first saw a truth table, it felt very CS-textbook. But once I started writing real conditions in my code, I referred back to this mental model constantly.
4. Assignment Operators — Setting (and Updating) Values
You already know =. It assigns a value to a variable.
let score = 10; // assigns 10 to score
But there are shorter ways to update a variable using assignment operators:
| Operator | What It Does | Example | Same As |
|---|---|---|---|
= |
Assign | x = 5 |
— |
+= |
Add and assign | x += 3 |
x = x + 3 |
-= |
Subtract and assign | x -= 2 |
x = x - 2 |
*= |
Multiply and assign | x *= 4 |
x = x * 4 |
/= |
Divide and assign | x /= 2 |
x = x / 2 |
let score = 10;
score += 5; // score is now 15
score -= 3; // score is now 12
score *= 2; // score is now 24
console.log(score); // 24
I started using += and -= a lot once I realized it made score-tracking, counters, and loops so much cleaner to write.
5. Key Discoveries Along the Way
A few things that surprised me or saved me later:
===over==— always. Loose equality has caused so many sneaky bugs in real projects. Strict equality is your friend.%is more useful than it looks. Checking even/odd, cycling through items in a list, pagination logic — the modulus operator shows up everywhere.!can be stacked.!!valueis a common trick to convert any value into a proper boolean. Weird at first, useful once you know it.Logical operators don't always return
trueorfalse. They return one of the actual values. This one's a bit more advanced, but keep it in mind for later.
6. Let's Put It All Together — Quick Assignment
Here's a small exercise I'd suggest trying yourself. It covers everything we talked about:
// --- Arithmetic ---
let a = 15;
let b = 4;
console.log("Sum:", a + b); // 19
console.log("Difference:", a - b); // 11
console.log("Product:", a * b); // 60
console.log("Quotient:", a / b); // 3.75
console.log("Remainder:", a % b); // 3
// --- Comparison ---
console.log(a == "15"); // true (loose)
console.log(a === "15"); // false (strict)
console.log(a > b); // true
console.log(a < b); // false
// --- Logical ---
let isAdult = a > 18; // false
let hasPermission = true;
if (!isAdult && hasPermission) {
console.log("Minor with permission — allowed conditionally.");
}
if (isAdult || hasPermission) {
console.log("At least one condition passed — allowed!");
}
Try running this in your browser console or a quick Node.js script. Then change the values of a and b and see how the outputs shift. That's the best way to really internalize it.
About the Author
Saurabh Prajapati is a Full-Stack Software Engineer at IBM India Software Lab, currently building cloud-native enterprise solutions on Maximo. He specializes in GenAI, React, and modern web technologies — and loves documenting his learning journey for other developers.
🐙 GitHub: prajapatisaurabh
💼 LinkedIn: saurabh-prajapati
📧 Email: saurabhprajapati120@gmail.com


