TechLead
Lesson 4 of 16

Operators

Combine values with arithmetic and comparisons.

Quick Summary

Operators perform work on values: arithmetic (+, -, *, /), comparisons (>, ===), and logical checks (&&, ||). In JavaScript, some operators can coerce types, so prefer strict comparisons (===) when you want predictable results.

Types of Operators

Arithmetic Operators

Perform mathematical calculations:

| Operator | Name | Example | Result | |----------|------|---------|--------| | + | Addition | 5 + 3 | 8 | | - | Subtraction | 5 - 3 | 2 | | * | Multiplication | 5 * 3 | 15 | | / | Division | 15 / 3 | 5 | | % | Modulo (remainder) | 17 % 5 | 2 | | ** | Exponentiation | 2 ** 3 | 8 |

Assignment Operators

Assign and modify values:

let x = 10;    // Assignment
x += 5;        // x = x + 5 → 15
x -= 3;        // x = x - 3 → 12
x *= 2;        // x = x * 2 → 24
x /= 4;        // x = x / 4 → 6
x++;           // x = x + 1 → 7 (increment)
x--;           // x = x - 1 → 6 (decrement)

Comparison Operators

Compare values and return boolean:

| Operator | Meaning | Example | |----------|---------|---------| | === | Strict equal | 5 === 5 → true | | !== | Strict not equal | 5 !== "5" → true | | == | Loose equal (avoid) | 5 == "5" → true | | > | Greater than | 5 > 3 → true | | < | Less than | 3 < 5 → true | | >= | Greater or equal | 5 >= 5 → true | | <= | Less or equal | 3 <= 5 → true |

Logical Operators

Combine boolean expressions:

// AND - both must be true
true && true   // true
true && false  // false

// OR - at least one must be true
true || false  // true
false || false // false

// NOT - inverts the value
!true  // false
!false // true

String Operators

const firstName = "John";
const lastName = "Doe";
const fullName = firstName + " " + lastName; // Concatenation

Try It Yourself

Here's a practical example you can try. Copy this code and run it in your browser's console (press F12 to open developer tools) or in the Code Playground.

// Arithmetic operators
const sum = 10 + 5;        // 15
const difference = 10 - 5;  // 5
const product = 10 * 5;     // 50
const quotient = 10 / 5;    // 2
const remainder = 17 % 5;   // 2 (modulo)
const power = 2 ** 4;       // 16 (exponentiation)

// Comparison operators (prefer strict ===)
const isEqual = 5 === 5;           // true
const isNotEqual = 5 !== "5";      // true (different types)
const isGreater = 10 > 5;          // true
const isLessOrEqual = 5 <= 5;      // true

// Logical operators
const hasAccess = isLoggedIn && hasPermission; // AND
const showBanner = isNew || isPromo;           // OR
const isHidden = !isVisible;                   // NOT

// Compound assignment
let score = 100;
score += 10;  // score is now 110
score -= 20;  // score is now 90
score *= 2;   // score is now 180

Key Takeaways

  • Combine values with arithmetic and comparisons.
  • Practice with real code examples to solidify your understanding
  • This concept builds the foundation for more advanced topics

Related Learning Resources

Continue your programming journey with these related tutorials: