Concept 4 of 10 Beginner ⏱️ 15 min

Operators

Learn how to perform calculations, combine text, and compare values using operators.

Operators are special symbols that tell the computer to perform specific operations. They're like the buttons on a calculator—each one does something different with your data.

Arithmetic Operators

These are your basic math operators. If you've used a calculator, you already know most of these!

Operator Name Example Result
+ Addition 5 + 3 8
- Subtraction 5 - 3 2
* Multiplication 5 * 3 15
/ Division 10 / 4 2.5
% Modulo (Remainder) 10 % 3 1
** Exponentiation 2 ** 3 8
💡

The Modulo Operator (%)

The modulo operator gives you the remainder of a division. It's super useful for checking if a number is even (n % 2 === 0) or for wrapping around values (like hours on a clock).

Try It Yourself

String Concatenation

When you use + with strings, it joins them together. This is called concatenation.

let firstName = "John";
let lastName = "Doe";
let fullName = firstName + " " + lastName;
// Result: "John Doe"

// Modern way using template literals
let greeting = `Hello, ${firstName}!`;
// Result: "Hello, John!"

Comparison Operators

Comparison operators compare two values and return true or false.

Operator Meaning Example Result
=== Equal to 5 === 5 true
!== Not equal to 5 !== 3 true
> Greater than 5 > 3 true
< Less than 5 < 3 false
>= Greater than or equal 5 >= 5 true
<= Less than or equal 3 <= 5 true
⚠️

=== vs ==

Always use === (strict equality) instead of ==. The triple equals checks both value AND type, which prevents bugs. For example: 5 == "5" is true, but 5 === "5" is false.

Logical Operators

Logical operators let you combine multiple conditions:

&&
AND
Both must be true
true && true = true
||
OR
At least one must be true
true || false = true
!
NOT
Flips the value
!true = false
let age = 25;
let hasLicense = true;

// AND: both conditions must be true
let canDrive = age >= 16 && hasLicense;  // true

// OR: at least one condition must be true
let canEnter = age >= 21 || hasVIPPass;

// NOT: flips true to false (and vice versa)
let isMinor = !isAdult;  // If isAdult is true, isMinor is false

Assignment Operators

Besides the basic =, there are shorthand operators for common operations:

let score = 10;

score += 5;   // Same as: score = score + 5  → 15
score -= 3;   // Same as: score = score - 3  → 12
score *= 2;   // Same as: score = score * 2  → 24
score /= 4;   // Same as: score = score / 4  → 6

// Increment and Decrement
score++;      // Same as: score = score + 1  → 7
score--;      // Same as: score = score - 1  → 6

Quick Quiz

Key Takeaways

  • Arithmetic operators (+, -, *, /, %) do math
  • The + operator also joins strings together
  • Comparison operators (===, !==, >, <) return true/false
  • Logical operators (&&, ||, !) combine conditions
  • Shorthand operators like += make code cleaner

What's Next?

Now that you can work with data and operators, let's learn about conditionals— how to make your code choose different paths based on conditions!

Finished this concept?

Mark it complete to track your progress