# Operators in JavaScript

## Why Do We Need Operators?

Imagine you’re building a small calculator or even a login system.

*   You need to **add numbers**
    
*   You need to **compare values**
    
*   You need to **make decisions** (like “if user is logged in AND has access”)
    

That’s where **operators** come in.

Operators are just **symbols that perform actions on values**.

Think of them like tools:

*   `+` → adds numbers
    
*   `>` → checks which number is bigger
    
*   `&&` → checks multiple conditions
    

* * *

## What Are Operators?

Operators are symbols used to **perform operations on data**.

Simple example:

```js
let result = 2 + 3;
console.log(result); // 5
```

Here:

*   `2` and `3` are values
    
*   `+` is the operator
    

* * *

## Types of Operators We’ll Learn

![Image](https://images.openai.com/static-rsc-4/lRELLGWrqwmRdCGLqenFeKBFW9VtJgH5Xp8Z4dJ4TWsTMqB8-h9TenCGCIH0TUPVMev5XcGzpEp5rNa35X1Vfkgv5UBTHLJRHOGMgOpj37rwycZwxnPv_8yh5mwJBsVwb4cRQNTeZ8EbB3kZGx1cXoQTx1j5DAP5KGwUx09k-PYHCzB5l3eWkswJFT6IbtHZ?purpose=fullsize align="center")

We’ll cover:

*   Arithmetic
    
*   Comparison
    
*   Logical
    
*   Assignment
    

* * *

## Arithmetic Operators (Basic Math)

These are the ones you already know from school.

```js
let a = 10;
let b = 3;

console.log(a + b); // 13 (addition)
console.log(a - b); // 7  (subtraction)
console.log(a * b); // 30 (multiplication)
console.log(a / b); // 3.33 (division)
console.log(a % b); // 1 (remainder)
```

### Real-Life Analogy

*   `+` → total money you have
    
*   `-` → money spent
    
*   `%` → leftover after dividing something
    

Example: If you divide 10 chocolates among 3 people → 1 is left → `%`

* * *

## Comparison Operators (Checking Values)

These operators compare values and return **true or false**.

```js
console.log(5 > 3);  // true
console.log(5 < 3);  // false
console.log(5 == '5'); // true
console.log(5 === '5'); // false
```

* * *

## Important: `==` vs `===`

This is where beginners often get confused.

```js
console.log(5 == '5');  // true
console.log(5 === '5'); // false
```

![Image](https://images.openai.com/static-rsc-4/lRELLGWrqwmRdCGLqenFeKBFW9VtJgH5Xp8Z4dJ4TWsTMqB8-h9TenCGCIH0TUPVMev5XcGzpEp5rNa35X1Vfkgv5UBTHLJRHOGMgOpj37rwycZwxnPv_8yh5mwJBsVwb4cRQNTeZ8EbB3kZGx1cXoQTx1j5DAP5KGwUx09k-PYHCzB5l3eWkswJFT6IbtHZ?purpose=fullsize align="center")

### Why?

*   `==` → checks value only (ignores type)
    
*   `===` → checks value AND type
    

### Simple Way to Remember

*   `==` → “loose check”
    
*   `===` → “strict check” (recommended)
    

* * *

## Logical Operators (Combining Conditions)

Used when you have multiple conditions.

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

console.log(age > 18 && hasID); // true
console.log(age > 18 || false); // true
console.log(!true); // false
```

* * *

## How They Work

![Image](https://images.openai.com/static-rsc-4/E3WCc3q6SJfIhbhVHOaS2pu9XH2kT-zQPfNvKpIs3gVZU4TClFsH3traZctA9NotEHbpnh1AaRAKcXZWSqN-0FdvEMehDWGj8drwTcIiOpi3Ey1__Uqq-fdrzy3mZXDdzQ-TaufAtbLoH84un_dpdOMwzoXesOSsLMG88f0nQPmTN8Lgamri1noLCSxtIktQ?purpose=fullsize align="center")

![Image](https://images.openai.com/static-rsc-4/tb4aenCvtZZmDJgcTiAgwspS5OC2uBEnZu1sh2PL4vOaoQ3VPjhOUGUeYY8lkoaZA9zdq89zPOtDrfsh6JMG1xb2RSAGmpIAgL4qAPwSB9hLVKtKYqelB8nYxjSZAs1mhElUgMCy-sqFdTcZFqZCc6-PqngdnlV8UJ5DIQCh7GbmGh4JSngPNr_eJVq4LhmX?purpose=fullsize align="center")

### AND (`&&`)

Both must be true

```js
true && true  // true
true && false // false
```

### OR (`||`)

At least one must be true

```js
true || false // true
false || false // false
```

### NOT (`!`)

Reverses value

```js
!true  // false
!false // true
```

* * *

## Assignment Operators (Storing Values)

Basic one:

```js
let x = 10;
```

Now shortcut versions:

```js
x += 5; // x = x + 5 → 15
x -= 3; // x = x - 3 → 12
```

![Image](https://images.openai.com/static-rsc-4/EuLYQkZGddYI52oABjuFY2mgwvA-bHnqeLNzbOr1fap6IUVu9SUqnFDO0ehVQQKh3hv_Bd38ibeOHbE7GNA7dN8lzTfcyvMEsFgkvqWOFdNTU7zw8xIKzw-Tge0Sg6r9z95FLUppqCEmCEmj6fMoZutxcQ3q-eDetMVRHRF8xIbtqcHJhA_okpZ0upSuOr4R?purpose=fullsize align="center")

### Why Use Them?

They make code shorter and cleaner.

* * *

## Before vs After (Cleaner Code)

### Without Shortcut

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

### With Shortcut

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

Same result, cleaner code.

* * *

## Real-Life Example (Putting It Together)

```js
let marks = 75;

if (marks >= 50 && marks <= 100) {
  console.log("Passed");
}
```

Here:

*   `>=` and `<=` → comparison
    
*   `&&` → logical
    

* * *

## Practice Assignment

Try these step by step:

### 1\. Arithmetic Operations

```js
let a = 8;
let b = 2;

// perform +, -, *, /, %
```

* * *

### 2\. Compare Values

```js
console.log(10 == '10');
console.log(10 === '10');
```

Observe the difference.

* * *

### 3\. Logical Condition

```js
let age = 22;
let hasLicense = true;

// check if person can drive
```

* * *

### 4\. Assignment Practice

```js
let score = 50;

// increase by 10 using +=
// decrease by 5 using -=
```
