# JavaScript operator

## What Are Operators?

In simple words:

> Operators are symbols that perform operations on values.

Example:

```plaintext
5 + 3
```

Here:

*   `5` and `3` → operands
    
*   `+` → operator
    

The operator tells JavaScript what to do with the operands.

* * *

## 1️⃣ Arithmetic Operators

Used for mathematical calculations.

| Operator | Meaning |
| --- | --- |
| `+` | Addition |
| `-` | Subtraction |
| `*` | Multiplication |
| `/` | Division |
| `%` | Modulus (remainder) |

* * *

➤ Addition (+)

```plaintext
let a = 10;
let b = 5;
console.log(a + b); // 15
```

* * *

➤ Subtraction (-)

```plaintext
console.log(a - b); // 5
```

* * *

➤ Multiplication (\*)

```plaintext
console.log(a * b); // 50
```

* * *

➤ Division (/)

```plaintext
console.log(a / b); // 2
```

* * *

➤ Modulus (%)

Returns the remainder.

```plaintext
console.log(10 % 3); // 1
```

10 divided by 3 leaves remainder 1.

Very useful for:

*   Checking even or odd numbers
    

```plaintext
let number = 4;

if (number % 2 === 0) {
  console.log("Even number");
}
```

* * *

# 2️⃣ Comparison Operators

Used to compare values.

They always return **true** or **false**.

| Operator | Meaning |
| --- | --- |
| `==` | Equal (loose comparison) |
| `===` | Strict equal |
| `!=` | Not equal |
| `>` | Greater than |
| `<` | Less than |

* * *

## ➤ `==` (Loose Equality)

Compares values but ignores type.

```plaintext
console.log(5 == "5"); // true
```

Why?

Because JavaScript converts the string `"5"` into number `5`.

This is called **type coercion**.

* * *

## ➤ `===` (Strict Equality)

Compares value AND type.

```plaintext
console.log(5 === "5"); // false
```

Here:

*   5 → number
    
*   "5" → string
    

Different types → false.

🔥 Always prefer `===` in real projects.

* * *

## ➤ `!=` (Not Equal)

```plaintext
console.log(10 != 5); // true
console.log(5 != "5"); // false
```

Because `"5"` becomes 5 during loose comparison.

* * *

## ➤ Greater Than / Less Than

```plaintext
console.log(10 > 5);  // true
console.log(3 < 2);   // false
```

* * *

## 3️⃣ Logical Operators

Used to combine multiple conditions.

| Operator | Meaning |
| --- | --- |
| `&&` | AND |
| \` |  |
| `!` | NOT |

These are mostly used inside `if` statements.

* * *

## ➤ Logical AND (&&)

Returns true only if BOTH conditions are true.

```plaintext
let age = 20;
let hasID = true;

if (age >= 18 && hasID) {
  console.log("You can enter");
}
```

If either condition is false → result is false.

* * *

### Truth Table for AND (&&)

| A | B | A && B |
| --- | --- | --- |
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |

* * *

## ➤ Logical OR (||)

Returns true if at least one condition is true.

```plaintext
let isAdmin = false;
let isEditor = true;

if (isAdmin || isEditor) {
  console.log("Access granted");
}
```

* * *

### Truth Table for OR (||)

| A | B | A || B |

|---|---|--------|

| true | true | true |

| true | false | true |

| false | true | true |

| false | false | false |

* * *

## ➤ Logical NOT (!)

Reverses the value.

```plaintext
let isLoggedIn = false;

console.log(!isLoggedIn); // true
```

It flips true ↔ false.

* * *

## 4️⃣ Assignment Operators

Used to assign values to variables.

| Operator | Meaning |
| --- | --- |
| `=` | Assign value |
| `+=` | Add and assign |
| `-=` | Subtract and assign |

* * *

## ➤ Basic Assignment (=)

```plaintext
let score = 10;
```

* * *

## ➤ Add and Assign (+=)

```plaintext
let score = 10;
score += 5;

console.log(score); // 15
```

Same as:

```plaintext
score = score + 5;
```

* * *

## ➤ Subtract and Assign (-=)

```plaintext
let score = 10;
score -= 3;

console.log(score); // 7
```

* * *

## Assignment Practice Example

Here’s a small combined example:

```plaintext
let num1 = 20;
let num2 = 5;

// Arithmetic
console.log(num1 + num2); // 25
console.log(num1 - num2); // 15
console.log(num1 * num2); // 100
console.log(num1 / num2); // 4

// Comparison
console.log(num1 == "20");  // true
console.log(num1 === "20"); // false

// Logical
let isAdult = true;
let hasTicket = false;

console.log(isAdult && hasTicket); // false
console.log(isAdult || hasTicket); // true
```

* * *

## Operator Categories Table

| Category | Operators |
| --- | --- |
| Arithmetic | \+ - \* / % |
| Comparison | \== === != > < |
| Logical | && |
| Assignment | \= += -= |

if you still do not get it , watch this below video but remember dont fall in tutorial hell

%[https://youtu.be/zjGwdWF0jhE]
