Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

Updated
โ€ข7 min read
JavaScript Operators: The Basics You Need to Know
A
Web Developer

๐Ÿงฎ 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 work

  • With === โ†’ 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?

  • == converts false โ†’ 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