# JavaScript Operators: The Basics You Need to Know

## 1\. What are Operators?

If variables are the "nouns" of our code (the things), operators are the "verbs" (the actions). They take one or more values and produce a result.

* * *

## 2\. Arithmetic Operators: The Math Basics

These are the ones you already know from school. We use them for everything from calculating the total in a shopping cart to determining the position of an element on a screen.

*   **Addition (+)**
    
*   **Subtraction (-)**
    
*   **Multiplication (\*)**
    
*   **Division (/)**
    
*   **Remainder (%)**: This gives you what is left over after a division.
    

**Real-World Example:**

JavaScript

```plaintext
let pricePerItem = 500;
let quantity = 3;
let shippingFee = 50;

let subtotal = pricePerItem * quantity; // 1500
let totalBill = subtotal + shippingFee; // 1550

// Check if a number is even using Remainder
let isEven = (totalBill % 2 === 0); 
```

* * *

## 3\. Comparison Operators: Making Decisions

Comparison operators are the backbone of logic. They look at two values and return either `true` or `false`.

### The "Double Equals" vs. "Triple Equals"

This is the most common mistake for beginners.

*   `==` **(Abstract Equality):** It checks the **value** but ignores the **data type**. It tries to force the values to be the same type before comparing.
    
*   `===` **(Strict Equality):** It checks both the **value** and the **data type**.
    

**Console Example:**

JavaScript

```plaintext
console.log(5 == "5");  // true (Value is same, type is ignored)
console.log(5 === "5"); // false (Value is same, but Number is not String)
```

**Pro-tip:** In the Web Dev Cohort 2026, we always aim for `===` to avoid hidden bugs.

**Other Comparisons:**

*   `!=` (Not equal)
    
*   `>` (Greater than)
    
*   `<` (Less than)
    

* * *

## 4\. Logical Operators: The Gatekeepers

Logical operators allow us to combine multiple conditions. This is how we handle complex scenarios, like checking if a user is both logged in **and** has a premium subscription.

*   `&&` **(AND):** True only if **both** sides are true.
    
*   `||` **(OR):** True if **at least one** side is true.
    
*   `!` **(NOT):** Reverses the value (true becomes false).
    

![truth table for logical operators, AI generated](https://encrypted-tbn2.gstatic.com/licensed-image?q=tbn:ANd9GcR28bVyYkiO-ztFR4gq2V1uzpUnDLIAAZILika1OVdz7Z7vDBL6s3axwf2IBVeUGJ_a_wilBxvJHY-DKlAd-beb0pRmZouoDSba6HZJ6HQPhQQ88sk align="center")

**Example:**

JavaScript

```plaintext
let isLoggedIn = true;
let hasSubscription = false;

// Can they watch the movie?
let canWatch = isLoggedIn && hasSubscription; // false
```

* * *

## 5\. Assignment Operators: The Shorthand

We use these to assign values to variables. You already know `=`, but there are "shortcut" versions that make your code much cleaner.

*   `=`: Basic assignment.
    
*   `+=`: Add and assign. (`x += 5` is the same as `x = x + 5`)
    
*   `-=`: Subtract and assign.
    

**Example:**

JavaScript

```plaintext
let score = 10;
score += 5; // score is now 15
score -= 2; // score is now 13
```

* * *

## Final Quest: The Operator Challenge

To finish this assignment, let's run a small script that combines all these concepts.

### Assignment Implementation

JavaScript

```plaintext
// 1. Arithmetic Operations
let num1 = 20;
let num2 = 10;
console.log("Sum:", num1 + num2);
console.log("Remainder:", num1 % 3);

// 2. Comparison (== vs ===)
let a = 10;
let b = "10";
console.log("Loose Comparison:", a == b);  // true
console.log("Strict Comparison:", a === b); // false

// 3. Logical Conditions
let temperature = 35;
let isSunny = true;

if (temperature > 30 && isSunny) {
  console.log("It is a hot sunny day.");
}

// 4. Assignment Shorthand
let wallet = 1000;
wallet -= 200; // Spent 200 on lunch
console.log("Remaining Balance:", wallet);
```

By mastering these operators, you have gained the tools to make your code dynamic. Instead of just storing data, you are now manipulating it.
