JavaScript Operators: The Basics You Need to Know

๐งฎ 1๏ธโฃ Arithmetic Operators
Used for mathematical calculations.
| Operator | Meaning | Example | Output |
|---|---|---|---|
โ + |
Addition | 5 + 3 |
8 |
โ - |
Subtraction | 5 - 3 |
2 |
โ๏ธ * |
Multiplication | 5 * 3 |
15 |
โ / |
Division | 6 / 2 |
3 |
๐ % |
Modulus (Remainder) | 7 % 2 |
1 |
โฌ๏ธ ** |
Exponent | 2 ** 3 |
8 |
๐ 2๏ธโฃ Assignment Operators
Used to assign values.
| Operator | Example | Same As |
|---|---|---|
= |
x = 10 |
Assign 10 |
+= |
x += 5 |
x = x + 5 |
-= |
x -= 5 |
x = x - 5 |
*= |
x *= 2 |
x = x * 2 |
/= |
x /= 2 |
x = x / 2 |
โ๏ธ 3๏ธโฃ Comparison Operators
Used to compare values (returns true / false).
| Operator | Meaning | Example | Output |
|---|---|---|---|
== |
Equal (loose) | 5 == "5" |
true |
=== |
Strict Equal | 5 === "5" |
false |
!= |
Not Equal | 5 != 3 |
true |
> |
Greater Than | 7 > 5 |
true |
< |
Less Than | 7 < 5 |
false |
๐ 4๏ธโฃ Logical Operators
Used to combine conditions.
| Operator | Meaning | Example | Output |
|---|---|---|---|
&& |
AND | true && false |
false |
| ` | ` | OR | |
! |
NOT | !true |
false |
โ 5๏ธโฃ Ternary Operator
Shortcut for ifโelse.
let result = age >= 18 ? "Adult" : "Minor";
๐ง Meaning:
If age โฅ 18 โ "Adult"
Else โ "Minor"
๐ 6๏ธโฃ Type Operators
| Operator | Example | Output |
|---|---|---|
typeof |
typeof "Hello" |
"string" |
instanceof |
arr instanceof Array |
true |
๐ฏ Why Operators Matter
โ Perform calculations
โ Make decisions
โ Control program logic
โ Compare values
โ Build real-world applications
โ๏ธ Difference Between == and === in JavaScript (Real-Life Examples)
๐น Simple Rule to Remember
| Operator | Name | Checks Value? | Checks Type? |
|---|---|---|---|
== |
Loose Equality | โ Yes | โ No (converts type) |
=== |
Strict Equality | โ Yes | โ Yes |
๐ == compares after converting types
๐ === compares without converting types
๐ฅ๏ธ Console Examples (Clear & Practical)
1๏ธโฃ Number vs String
console.log(5 == "5");
console.log(5 === "5");
Output:
true
false
๐ก Why?
==converts"5"โ number โ compares โ โ true===checks type also โ number vs string โ โ false
2๏ธโฃ User Age from Input Field (Real-Life Example)
In real projects (like forms you build), input values come as strings.
let age = "18"; // From input field
console.log(age == 18);
console.log(age === 18);
Output:
true
false
๐ง Real meaning:
With
==โ JS auto converts โ might workWith
===โ safer โ avoids hidden bugs
โ Best Practice: Convert properly
let age = Number("18");
console.log(age === 18); // true
3๏ธโฃ Login System Example
let savedPassword = 1234; // Number in database
let enteredPassword = "1234"; // User input
console.log(savedPassword == enteredPassword);
console.log(savedPassword === enteredPassword);
Output:
true
false
โ ๏ธ Using == here can be dangerous because it ignores type.
In real apps โ always use:
console.log(savedPassword === Number(enteredPassword));
4๏ธโฃ Boolean Example
console.log(0 == false);
console.log(0 === false);
Output:
true
false
๐ก Why?
==convertsfalseโ 0===sees number vs boolean โ false
This is where beginners get confused.
๐ Everyday Rule for Developers
โ Always use ===
โ Use == only if you truly understand type conversion
โ In real projects โ strict comparison avoids unexpected bugs
๐ฏ One-Line Memory Trick
== โ "Adjust and Compare"
=== โ "Exact Match Only"
๐ JavaScript Operator Categories (With Examples, Uses, Pros & Cons)
๐ 1๏ธโฃ Operator Categories Table
| ๐ Category | ๐ฃ Operators | ๐งช Example | โ Output | ๐ฏ Uses | ๐ Pros | ๐ Cons |
|---|---|---|---|---|---|---|
| ๐งฎ Arithmetic | + - * / % ** |
10 % 3 |
1 |
Calculations, totals, pricing logic | Simple & fast | % & ** confuse beginners |
| ๐ Assignment | = += -= *= /= |
let x=5; x+=2 |
7 |
Update values, counters | Clean shorthand | Hard to read if overused |
| โ๏ธ Comparison | == === != > < >= |
5 === "5" |
false |
Conditions, validations | Prevent logic errors | == can cause bugs |
| ๐ Logical | `&& | !` | true && false |
false |
Combine conditions | |
| โ Ternary | ? : |
age>=18?"Adult":"Minor" |
"Adult" |
Short ifโelse | Compact code | Hard to read if nested |
| ๐ Type | typeof instanceof |
typeof 10 |
"number" |
Type checking | Helps debugging | typeof null bug (object) |
๐ Logical Operators Truth Table (With Examples, Uses, Pros & Cons)
๐ง 1๏ธโฃ AND &&
| A | B | A && B | ๐งช Example | โ Output |
|---|---|---|---|---|
| true | true | true | true && true |
true |
| true | false | false | true && false |
false |
| false | true | false | false && true |
false |
| false | false | false | false && false |
false |
๐ฏ Uses: Login validation, multiple conditions required
๐ Pros: Ensures all conditions pass
๐ Cons: Can get messy with many conditions
Real-life example:isLoggedIn && isVerified โ User can access dashboard
๐ง 2๏ธโฃ OR ||
| A | B | A || B | ๐งช Example | โ
Output |
|---|---|--------|------------|----------|
| true | true | true | true || true | true |
| true | false | true | true || false | true |
| false | true | true | false || true | true |
| false | false | false | false || false | false |
๐ฏ Uses: Default values, fallback logic
๐ Pros: Flexible condition handling
๐ Cons: Can return unexpected values if misused
Real-life example:username || "Guest" โ Sets default username
๐ง 3๏ธโฃ NOT !
| A | !A | ๐งช Example | โ Output |
|---|---|---|---|
| true | false | !true |
false |
| false | true | !false |
true |
๐ฏ Uses: Toggle states, reverse conditions
๐ Pros: Simple inversion
๐ Cons: Double !! can confuse beginners
Real-life example:!isLoggedIn โ Show login button
๐ฏ Quick Everyday Summary
โ Arithmetic โ Math work
โ Assignment โ Store & update values
โ Comparison โ Make decisions
โ Logical โ Combine decisions
โ Ternary โ Short ifโelse
โ Type โ Check data type




