# JavaScript Operators: The Basics You Need to Know

Operators are one of the most fundamental parts of programming. In JavaScript, operators allow us to **perform calculations, compare values, and make logical decisions** in our programs.

In simple terms:

> **Operators are symbols that perform operations on values (operands).**

For example:

```plaintext
5 + 3
```

Here:

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

Let’s explore the most commonly used operators in JavaScript.

* * *

# Arithmetic Operators

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

| Operator | Meaning | Example |
| --- | --- | --- |
| + | Addition | 5 + 3 |
| \- | Subtraction | 10 - 4 |
| \* | Multiplication | 6 \* 2 |
| / | Division | 8 / 2 |
| % | Modulus (remainder) | 10 % 3 |

Example:

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

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

Output:

```plaintext
13
7
30
3.33
1
```

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

* * *

# Comparison Operators

Comparison operators are used to **compare two values**. They return a **boolean value (**`true` **or** `false`**)**.

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

Example:

```plaintext
let x = 10;
let y = "10";

console.log(x == y);
console.log(x === y);
```

Output:

```plaintext
true
false
```

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

| Operator | Behavior |
| --- | --- |
| \== | Compares values only |
| \=== | Compares value AND type |

Example:

```plaintext
10 == "10"   // true
10 === "10"  // false
```

Because `"10"` is a **string** while `10` is a **number**.

For safer code, developers usually prefer `===`.

* * *

# Logical Operators

Logical operators are used to **combine conditions**.

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

Example:

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

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

Output:

```plaintext
Allowed to enter
```

### Logical Operator Truth Table

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

The `!` operator simply **reverses a value**.

Example:

```plaintext
let isLoggedIn = false;

console.log(!isLoggedIn);
```

Output:

```plaintext
true
```

* * *

# Assignment Operators

Assignment operators are used to **assign values to variables**.

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

Example:

```plaintext
let score = 10;

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

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

Output:

```plaintext
15
12
```

Here:

```plaintext
score += 5  → score = score + 5
score -= 3  → score = score - 3
```

* * *

# Small Practice Example

Let’s combine multiple operators in a simple program.

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

console.log("Addition:", num1 + num2);
console.log("Multiplication:", num1 * num2);

console.log(num1 == num2);
console.log(num1 > num2);

if (num1 > 10 && num2 < 10) {
  console.log("Condition is true");
}
```

Output:

```plaintext
Addition: 20
Multiplication: 75
false
true
Condition is true
```

* * *

# Operator Categories (Quick Overview)

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

* * *

# Conclusion

Operators are essential for performing calculations and making decisions in JavaScript programs.

Key takeaways:

*   **Arithmetic operators** perform mathematical operations.
    
*   **Comparison operators** compare values and return `true` or `false`.
    
*   **Logical operators** combine conditions.
    
*   **Assignment operators** store and update values in variables.
    

By understanding these basic operators, you can start writing **more powerful and dynamic JavaScript programs**.
