# JavaScript Operators: The Basics You Need to Know

When writing JavaScript code, you often need to **perform calculations, compare values, or combine conditions**. To do these tasks, JavaScript uses something called **operators**.

If you’ve ever done basic math like `5 + 3`, you’ve already used an operator. In programming, operators help you **perform actions on values and variables**.

In this article, we’ll explore some of the **most common JavaScript operators** that every beginner should understand.

### What Are Operators?

An **operator** is a symbol that performs an operation on values.

For example:

```plaintext
let result = 10 + 5;
console.log(result);
```

Output:

```plaintext
15
```

In this example:

*   `10` and `5` are **values (operands)**
    
*   `+` is the **operator**
    
*   The operator performs the **addition**
    

JavaScript provides different types of operators depending on the task you want to perform.

Some of the most commonly used ones include:

*   Arithmetic operators
    
*   Comparison operators
    
*   Logical operators
    
*   Assignment operators
    

Let’s look at each of them with simple examples.

## Arithmetic Operators

Arithmetic operators are used for **basic mathematical calculations**, just like in regular math.

Example:

```plaintext
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
```

Here are the most common arithmetic operators:

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

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

Example:

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

Output:

```plaintext
1
```

This means **10 divided by 3 leaves a remainder of 1**.

## Comparison Operators

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

They return either:

*   `true`
    
*   `false`
    

Example:

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

console.log(a > b);
console.log(a < b);
```

Output:

```plaintext
true
false
```

Here are some common comparison operators:

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

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

This is one of the most important things beginners should understand.

#### Using `==`

`==` checks if two values are equal **after type conversion**.

Example:

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

Output:

```plaintext
true
```

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

#### Using `===`

`===` checks both **value and data type**.

Example:

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

Output:

```plaintext
false
```

Because:

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

For this reason, developers usually prefer using `===` to avoid unexpected results.

## Logical Operators

Logical operators are used when you want to **combine multiple conditions**.

These operators are commonly used in **conditional statements** like `if`.

Example:

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

console.log(age >= 18 && hasLicense);
```

Output:

```plaintext
true
```

Common logical operators include:

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

### AND (`&&`)

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

Example:

```plaintext
console.log(true && true);
```

Output:

```plaintext
true
```

### OR (`||`)

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

Example:

```plaintext
console.log(true || false);
```

Output:

```plaintext
true
```

### NOT (`!`)

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

Example:

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

Output:

```plaintext
false
```

## Assignment Operators

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

The most common one is the `=` operator.

Example:

```plaintext
let score = 50;
```

Here, the value **50 is assigned to the variable** `score`.

JavaScript also provides shorthand assignment operators that combine operations.

Example:

```plaintext
let score = 10;

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

Output:

```plaintext
15
```

Common assignment operators include:

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

Another example:

```plaintext
let points = 20;

points -= 5;

console.log(points);
```

Output:

```plaintext
15
```
