# JavaScript Operators: The Basics You Need to Know

When writing JavaScript programs, we often need to perform calculations, compare values, or make decisions.

This is where operators come in.

Operators are symbols that tell JavaScript to perform an operation on values or variables.

For example:

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

Here:

*   `5` and `3` are **values**
    
*   `+` is the **operator**
    
*   `8` is the **result**
    

JavaScript provides several types of operators, but in this article we'll focus on the most important beginner ones:

*   Arithmetic Operators
    
*   Comparison Operators
    
*   Logical Operators
    
*   Assignment Operators
    

These are the operators you’ll use almost every day in JavaScript.

## What are Operators ?

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

Example:

```javascript
let total = 10 + 5;
```

JavaScript reads this as:

```javascript
Take 10 and 5, add them together, and store the result in total
```

Operators help us to Perform calculations, Compare values, Assign data to variables, Create logical conditions.

Now let's explore each category.

## Arithmetic Operators

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

These are the most basic operators you'll use.

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

### Addition (+)

Adds two numbers.

```javascript
let result = 5 + 3;

console.log(result);
```

Output:

```plaintext
8
```

### Subtraction (-)

Subtracts one number from another.

```javascript
let result = 10 - 4;

console.log(result);
```

Output:

```plaintext
6
```

### Multiplication (\*)

Multiplies numbers.

```javascript
let result = 6 * 3;

console.log(result);
```

Output:

```plaintext
18
```

### Division (/)

Divides one number by another.

```javascript
let result = 20 / 5;

console.log(result);
```

Output:

```plaintext
4
```

### Modulus (%)

The **modulus operator** gives the **remainder after division**.

```javascript
let result = 10 % 3;

console.log(result);
```

Output:

```plaintext
1
```

Because:

```plaintext
10 ÷ 3 = 3 remainder 1
```

This operator is often used to check **even and odd numbers**.

Example:

```javascript
console.log(4 % 2);
```

Output:

```plaintext
0
```

If the remainder is **0**, the number is even.

## Comparison Operators

Comparison operators are used to **compare two values**.

They return either:

```javascript
true
or
false
```

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

### Equal To (==)

Checks if two values are equal **after type conversion**.

Example:

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

Output:

```plaintext
true
```

Why?

Because JavaScript converts `"5"` (string) into `5` (number) before comparing.

### Strict Equal (===)

Checks **both value and type**.

Example:

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

Output:

```plaintext
false
```

Here:

*   `5` is a **number**
    
*   `"5"` is a **string**
    

Since the types are different, the result is **false**.

### Difference Between == and ===

It is very important for to understand the difference between this two operators.

| Expression | Result | Reason |
| --- | --- | --- |
| 5 == "5" | true | value matches |
| 5 === "5" | false | type different |

Most developers recommend **using** `===` because it avoids unexpected results.

### Not Equal (!=)

Checks if values are **not equal**.

```javascript
console.log(5 != 3);
```

Output:

```plaintext
true
```

Because `5` is not equal to `3`.

### Greater Than (>)

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

Output:

```plaintext
true
```

### Less Than (<)

```javascript
console.log(3 < 8);
```

Output:

```plaintext
true
```

These operators are often used inside conditions and loops.

## Logical Operators

Logical operators help us combine multiple conditions.

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

### AND (&&)

The **AND operator** returns `true` only if both conditions are true.

Example:

```javascript
let age = 20;

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

Output:

```plaintext
true
```

Because:

```plaintext
20 > 18 → true
20 < 30 → true
```

Both are true.

### OR (||)

The **OR operator** returns `true` if **at least one condition is true**.

Example:

```javascript
let age = 16;

console.log(age < 18 || age > 60);
```

Output:

```plaintext
true
```

Because:

```plaintext
16 < 18 → true
```

Only one condition needs to be true.

### NOT (!)

The **NOT operator** reverses a boolean value.

Example:

```javascript
let isLoggedIn = true;

console.log(!isLoggedIn);
```

Output:

```plaintext
false
```

So `!` simply flips the result.

## Assignment Operators

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

The most common one is:

```plaintext
=
```

Example:

```javascript
let number = 10;
```

Here, the value **10 is assigned to number**.

### += Operator

Adds a value and assigns the result.

Example:

```javascript
let score = 10;

score += 5;

console.log(score);
```

Output:

```plaintext
15
```

This is the same as:

```javascript
score = score + 5;
```

### \-= Operator

Subtracts a value and assigns the result.

Example:

```javascript
let score = 10;

score -= 3;

console.log(score);
```

Output:

```plaintext
7
```

Equivalent to:

```javascript
score = score - 3;
```

These shorthand operators help keep code **shorter and cleaner**.

## Summary

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

These operators form the foundation of JavaScript programming.
