# JavaScript Operators: The Basics You Need to Know

When writing programs in **JavaScript**, we often perform operations such as:

*   Adding numbers
    
*   Comparing values
    
*   Checking conditions
    

To perform these actions, JavaScript provides **operators**.

Operators help us **manipulate values and perform calculations in code**.

* * *

# What Are Operators?

An **operator** is a symbol used to perform an operation on values.

Example:

```javascript
let result = 5 + 3;
```

Here:

*   `5` and `3` are **operands**
    
*   `+` is the **operator**
    

Output:

```plaintext
8
```

Operators allow JavaScript to perform **mathematical operations, comparisons, and logical checks**.

* * *

# Arithmetic Operators

Arithmetic operators are used to perform **mathematical calculations**.

| Operator | Meaning | Example |
| --- | --- | --- |
| `+` | Addition | `5 + 3` |
| `-` | Subtraction | `5 - 3` |
| `*` | Multiplication | `5 * 3` |
| `/` | Division | `6 / 3` |
| `%` | Modulus (remainder) | `7 % 2` |

Example:

```javascript
let a = 10;
let b = 5;

console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);
console.log(a % b);
```

Output:

```plaintext
15
5
50
2
0
```

The `%` operator returns the **remainder after division**.

Example:

```plaintext
7 % 2 = 1
```

* * *

# Comparison Operators

Comparison operators compare two values and return **true or false**.

| Operator | Meaning |
| --- | --- |
| `==` | Equal to |
| `===` | Strict equal |
| `!=` | Not equal |
| `>` | Greater than |
| `<` | Less than |

Example:

```javascript
console.log(5 > 3);
console.log(5 < 3);
console.log(5 == "5");
console.log(5 === "5");
```

Output:

```plaintext
true
false
true
false
```

* * *

# Difference Between `==` and `===`

This is a very important concept.

### `==` (Loose Equality)

Compares **values only**.

```javascript
console.log(5 == "5");
```

Output:

```plaintext
true
```

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

* * *

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

Compares **value AND data type**.

```javascript
console.log(5 === "5");
```

Output:

```plaintext
false
```

Because:

*   `5` → number
    
*   `"5"` → string
    

Best practice: **prefer** `===` **in most cases**.

* * *

# Logical Operators

Logical operators are used when **working with conditions**.

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

* * *

### Logical AND `&&`

Returns true only if **both conditions are true**.

```javascript
let age = 20;

console.log(age > 18 && age < 30);
```

Output:

```plaintext
true
```

* * *

### Logical OR `||`

Returns true if **at least one condition is true**.

```javascript
console.log(5 > 10 || 10 > 5);
```

Output:

```plaintext
true
```

* * *

### Logical NOT `!`

Reverses a boolean value.

```javascript
console.log(!true);
```

Output:

```plaintext
false
```

* * *

# Truth Table for Logical Operators

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

* * *

# Assignment Operators

Assignment operators assign values to variables.

| Operator | Example | Meaning |
| --- | --- | --- |
| `=` | `x = 5` | Assign value |
| `+=` | `x += 2` | Add and assign |
| `-=` | `x -= 2` | Subtract and assign |

Example:

```javascript
let x = 10;

x += 5;
console.log(x);

x -= 3;
console.log(x);
```

Output:

```plaintext
15
12
```

* * *

# Assignment Practice

## Perform Arithmetic Operations

```javascript
let a = 8;
let b = 4;

console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);
```

* * *

# Compare Values Using `==` and `===`

```javascript
console.log(10 == "10");
console.log(10 === "10");
```

Output:

```plaintext
true
false
```

* * *

# Logical Operator Example

```javascript
let age = 22;

if (age > 18 && age < 60) {
  console.log("Eligible to work");
}
```

Output:

```plaintext
Eligible to work
```

* * *

# Operator Categories Summary

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

* * *
