# Javascript Operators The Basic You need to Know : 

### Understanding Operators in JavaScript

While writing programs, we frequently perform operations on different values.

For example:

*   Adding two numbers
    
*   Comparing values
    
*   Updating a variable’s value
    
*   Combining multiple conditions
    

In JavaScript, the symbols used to perform these operations are called **operators**.

Operators are extremely important because they appear in almost every program we write.

* * *

### 1\. What Are Operators?

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

The values on which the operator works are called **operands**.

Example:

```javascript
let total = 7 + 3
```

Here:

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

The `+` symbol tells JavaScript to add the two numbers together.

Operators allow programs to **perform calculations, compare values, and control program logic**.

* * *

### 2\. Arithmetic Operators

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

These operations are similar to the basic math we use daily.

| Operator | Purpose |
| --- | --- |
| + | Addition |
| \- | Subtraction |
| \* | Multiplication |
| / | Division |
| % | Modulus (remainder) |

* * *

### Addition (+)

The addition operator adds two numbers.

Example:

```javascript
let x = 12
let y = 8

console.log(x + y)
```

Output:

```javascript
20
```

The `+` operator is widely used in programming.

* * *

### Subtraction (-)

The subtraction operator finds the difference between two numbers.

Example:

```javascript
let x = 15
let y = 6

console.log(x - y)
```

Output:

```plaintext
9
```

* * *

### Multiplication (\*)

Multiplication is used to multiply numbers.

Example:

```javascript
let x = 5
let y = 7

console.log(x * y)
```

Output:

```plaintext
35
```

* * *

### Division (/)

Division splits one number by another.

Example:

```javascript
let x = 30
let y = 6

console.log(x / y)
```

Output:

```plaintext
5
```

* * *

### Modulus (%)

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

Example:

```javascript
let x = 11
let y = 4

console.log(x % y)
```

Output:

```javascript
3
```

Explanation:

```javascript
11 ÷ 4 = 2 remainder 3
```

### Practical Example

This operator is often used to check whether a number is **even or odd**.

```javascript
let value = 12

console.log(value % 2)
```

Output:

```javascript
0
```

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

* * *

### 3\. Comparison Operators

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

They return a **boolean value**, which can be:

*   `true`
    
*   `false`
    

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

* * *

### Equal To (==)

The `==` operator checks whether two values are equal **after converting their types if necessary**.

Example:

```javascript
console.log(8 == "8")
```

Output:

```javascript
true
```

Why?

JavaScript converts the string `"8"` into the number `8` before comparing.

```javascript
8 == "8"
8 == 8
true
```

Because of this automatic conversion, `==` can sometimes produce unexpected results.

* * *

### Strict Equal (===)

The `===` operator checks **both the value and the data type**.

Example:

```javascript
console.log(8 === "8")
```

Output:

```javascript
false
```

Explanation:

*   `8` → number
    
*   `"8"` → string
    

Since the data types are different, the result becomes false.

* * *

### Difference Between <mark class="bg-yellow-200 dark:bg-yellow-500/30">and </mark> \=

Example:

```javascript
console.log(20 == "20")
console.log(20 === "20")
```

Output:

```javascript
true
false
```

| Operator | What it checks |
| --- | --- |
| \== | Value only |
| \=== | Value and type |

### Best Practice

In modern JavaScript, developers usually prefer using `===` because it avoids unexpected type conversions.

* * *

### Not Equal (!=)

This operator checks if two values are different.

Example:

```javascript
console.log(7 != 4)
```

Output:

```javascript
true
```

Because 7 and 4 are not equal.

* * *

### Greater Than (>) and Less Than (<)

These operators compare numbers.

Example:

```javascript
console.log(18 > 10)
```

Output:

```javascript
true
```

Another example:

```javascript
console.log(3 < 1)
```

Output:

```javascript
false
```

* * *

### 4\. Logical Operators

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

They are commonly used in **decision-making statements like if conditions**.

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

* * *

### Logical AND (&&)

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

Example:

```javascript
let temperature = 25
let isSunny = true

console.log(temperature > 20 && isSunny)
```

Output:

```javascript
true
```

Explanation:

*   `temperature > 20` → true
    
*   `isSunny` → true
    

Both conditions are true, so the final result is **true**.

* * *

### Logical OR (||)

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

Example:

```javascript
let hasCoupon = false
let isMember = true

console.log(hasCoupon || isMember)
```

Output:

```javascript
true
```

Since one condition is true, the result becomes true.

* * *

### Logical NOT (!)

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

Example:

```javascript
let isOnline = true

console.log(!isOnline)
```

Output:

```javascript
false
```

* * *

### 5\. Assignment Operators

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

The most basic assignment operator is:

```javascript
=
```

* * *

### Basic Assignment (=)

Example:

```javascript
let points = 50
```

Here the value **50 is assigned to the variable** `points`.

* * *

### Addition Assignment (+=)

This operator adds a value to the variable and updates it.

Example:

```javascript
let points = 10

points += 3
```

This is the same as writing:

```javascript
points = points + 3
```

Final value:

```javascript
13
```

* * *

### Subtraction Assignment (-=)

This operator subtracts a value from a variable and updates it.

Example:

```javascript
let points = 25

points -= 5
```

Equivalent to:

```javascript
points = points - 5
```

Final value:

```javascript
20
```

These shorthand operators make the code **shorter and easier to read**.
