Operators

Loading concept...

JavaScript Operators: The Magic Wands of Code ✨

Imagine you’re a wizard with a toolbox of magic wands. Each wand does something special - one adds things together, another compares sizes, and some even check if two things are exactly the same. In JavaScript, these magic wands are called operators.


🧮 Arithmetic Operators: Your Math Helpers

Think of arithmetic operators like the buttons on a calculator. They help you do math!

The Five Basic Helpers

Operator Name What It Does
+ Addition Adds numbers together
- Subtraction Takes away
* Multiplication Makes copies
/ Division Shares equally
% Modulus Finds the leftover

Let’s See Them Work!

let apples = 5 + 3;   // 8 apples
let left = 10 - 4;    // 6 left
let total = 6 * 2;    // 12 total
let each = 20 / 4;    // 5 each
let extra = 17 % 5;   // 2 extra

The Modulus Trick: When you divide 17 cookies among 5 friends, each gets 3 cookies. But wait - there are 2 extra! That’s what % finds.

17 % 5 = 2  // 17 ÷ 5 = 3, leftover = 2

📝 Assignment Operators: Putting Things in Boxes

Assignment operators are like putting toys in a toy box. The box (variable) holds whatever you put in it.

The Basic Box-Filler

let score = 0;  // Put 0 in the box

Shortcut Helpers

Instead of writing long code, use shortcuts!

Long Way Shortcut Meaning
x = x + 5 x += 5 Add 5 to x
x = x - 3 x -= 3 Take 3 from x
x = x * 2 x *= 2 Double x
x = x / 4 x /= 4 Divide x by 4
x = x % 3 x %= 3 Keep remainder
let coins = 10;
coins += 5;   // Now 15 coins
coins -= 2;   // Now 13 coins
coins *= 2;   // Now 26 coins

🔄 Increment & Decrement: The Quick Counters

Counting up or down by 1? These are your best friends!

Counting Up (Increment)

let count = 5;
count++;  // Now 6 (add 1)

Counting Down (Decrement)

let lives = 3;
lives--;  // Now 2 (minus 1)

The Tricky Part: Before or After?

Where you put ++ matters!

let a = 5;
let b = a++;  // b = 5, then a = 6
// (use first, add after)

let c = 5;
let d = ++c;  // c = 6 first, d = 6
// (add first, then use)

Simple Rule:

  • x++ = use the old value, then add
  • ++x = add first, then use the new value

⚖️ Comparison Operators: The Judge

Comparison operators answer yes/no questions. They return true or false.

Meet the Judges

Operator Question It Asks
> Is left bigger?
< Is left smaller?
>= Is left bigger or equal?
<= Is left smaller or equal?
== Are they similar?
=== Are they exactly the same?
!= Are they different?
!== Are they truly different?
5 > 3    // true (5 is bigger)
2 < 1    // false (2 is not smaller)
5 >= 5   // true (equal counts!)
10 <= 9  // false

🎯 Strict vs Loose Equality: The Twin Test

This is super important! Let’s understand with a story.

Loose Equality (==) - The Friendly Check

Loose equality is like asking: “Do these look similar?”

It tries to convert things to match before comparing.

5 == "5"    // true!
// (Converts "5" to 5, then compares)

0 == false  // true!
// (Converts false to 0)

null == undefined  // true!

Strict Equality (===) - The Exact Check

Strict equality asks: “Are these EXACTLY the same type AND value?”

5 === "5"    // false!
// (Number vs String - different types)

0 === false  // false!
// (Number vs Boolean - different types)

5 === 5      // true!
// (Same type, same value)

The Golden Rule

Always use === and !== unless you have a specific reason not to. It prevents sneaky bugs!

graph TD A[Comparing Two Values?] --> B{Same Type?} B -->|Yes| C{Same Value?} B -->|No| D[=== returns false] C -->|Yes| E[=== returns true] C -->|No| F[=== returns false] D --> G[== might still be true]

🎢 Operator Precedence: Who Goes First?

When you have multiple operators, JavaScript follows rules about who goes first - just like in math class!

The Order (High to Low)

  1. Parentheses () - Always first!
  2. Increment/Decrement ++ --
  3. Multiply/Divide/Modulus * / %
  4. Add/Subtract + -
  5. Comparisons < > <= >=
  6. Equality == === != !==
  7. Assignment = += -= etc.

See It In Action

let result = 2 + 3 * 4;
// Step 1: 3 * 4 = 12
// Step 2: 2 + 12 = 14
// result = 14

let clear = (2 + 3) * 4;
// Step 1: 2 + 3 = 5 (parentheses first!)
// Step 2: 5 * 4 = 20
// clear = 20

The Safety Tip

When in doubt, use parentheses! They make your code clear and prevent mistakes.

// Confusing
let x = 5 + 3 * 2 - 1;

// Clear
let x = 5 + (3 * 2) - 1;

🌟 Quick Memory Helpers

Topic Remember This
% Modulus Finds the leftover after division
+= and friends Shortcuts that update and save
x++ vs ++x After vs Before
== Loose Converts, then compares
=== Strict No converting - must match exactly
Precedence When confused, add ()

🎮 Try This!

What does this code produce?

let x = 10;
x += 5;        // x = ?
let y = x++;   // y = ?, x = ?
let z = x === 16;  // z = ?

Answers:

  • After x += 5: x = 15
  • After let y = x++: y = 15, x = 16
  • z = true (16 === 16)

You did it! You now understand JavaScript operators. They’re your tools for doing math, saving values, counting, comparing, and controlling how your code runs. Use them wisely, young wizard! 🧙‍♂️

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.