# JavaScript Operators: The Basics You Need to Know

In our everyday life, we have to do a lot of calculation, some are calculating their salaries, someone will make budget for the month, expense etc. And as we know to perform this calculations, we use mathematical symbols like  addition(+), subtraction(-), multiplication (8) and etc. It helps us to do our calculation fast and at ease. Programming languages works in a similar way. To perform mathematical calculation we use operators like +, //, %, etc. So In today's blog we are going to understand what are operator and how to use them.

> ##  **What are operators**

Operators are the essential symbols that instructs a computer ( compilaer or interpretor )  to perform special mathematical operator and produce solution.

```javascript
1 + 3 = 4 // 1,3 are the operands and + is the operator
```

Operands are the value which is to be calculated and operators tell which mathematical operation will be done. 

![](https://cdn.hashnode.com/uploads/covers/69078637d192b5f0327809a3/1772ee98-beff-4eeb-8e8e-aa52dd15ed2e.png align="center")

> ### Arithmetic operators

Arithmetic operators includes **+, -, \*, /, %** and are hte most commonly used operators. It is used to do basic mathematical operations like addition, subtraction and mutlipication etc .

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

console.log(a + b) // output = 15, add the values
console.log(b - a) // output = 5, subtracts the values
console.log(a * b) // output = 50, multiplies them
console.log(b / a) // output = 0, gives quotient
console.log(a % b) // output = 5, gives remainder
```

> ### Comparison operators

Comparison operators are **\==, ===, !=, >, <** and it compares to the values and return the Boolean value . It returns true of the comparisons turns out to be true, otherwise false.

```javascript
let a = 1;

console.log(a == '1')
// true, double equals checks the values

console.log(a === '1')
// false, because a is number and c is string, triple equals checks the values along with with data types

console.log(a != 4)
// true, because the statement is true 

console.log(a > 4)
// false, because a is smaller than b 

console.log(a < 4)
// true, because a is smaller than b 

console.log(a <= 1)
// true, because 1 is equals to a
```

> ## Logical operators

![](https://cdn.hashnode.com/uploads/covers/69078637d192b5f0327809a3/65696da2-0c6a-4eda-8e13-9b24522c8b8f.png align="center")

1.  **Logical AND (&&)**
    

The **AND operator** returns **true only when both conditions are true**.

<table style="min-width: 75px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><td colspan="1" rowspan="1"><p><strong>A</strong></p></td><td colspan="1" rowspan="1"><p><strong>B</strong></p></td><td colspan="1" rowspan="1"><p><strong>A &amp;&amp; B</strong></p></td></tr><tr><td colspan="1" rowspan="1"><p>true</p></td><td colspan="1" rowspan="1"><p>true</p></td><td colspan="1" rowspan="1"><p>true</p></td></tr><tr><td colspan="1" rowspan="1"><p>true</p></td><td colspan="1" rowspan="1"><p>false</p></td><td colspan="1" rowspan="1"><p>false</p></td></tr><tr><td colspan="1" rowspan="1"><p>false</p></td><td colspan="1" rowspan="1"><p>true</p></td><td colspan="1" rowspan="1"><p>false</p></td></tr><tr><td colspan="1" rowspan="1"><p>false</p></td><td colspan="1" rowspan="1"><p>false</p></td><td colspan="1" rowspan="1"><p>false</p></td></tr></tbody></table>

**Both conditions must be true for the result to be true.**

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

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

1.  **Logical OR (||)**
    

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

<table style="min-width: 100px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><td colspan="1" rowspan="1"><p><strong>A</strong></p></td><td colspan="1" rowspan="1"><p><strong>B</strong></p></td><td colspan="1" rowspan="1"><p><strong>&nbsp;</strong></p></td><td colspan="1" rowspan="1"><p><strong>A || B</strong></p></td></tr><tr><td colspan="1" rowspan="1"><p>true</p></td><td colspan="1" rowspan="1"><p>true</p></td><td colspan="1" rowspan="1"><p>&nbsp;</p></td><td colspan="1" rowspan="1"><p>true</p></td></tr><tr><td colspan="1" rowspan="1"><p>true</p></td><td colspan="1" rowspan="1"><p>false</p></td><td colspan="1" rowspan="1"><p>&nbsp;</p></td><td colspan="1" rowspan="1"><p>true</p></td></tr><tr><td colspan="1" rowspan="1"><p>false</p></td><td colspan="1" rowspan="1"><p>true</p></td><td colspan="1" rowspan="1"><p>&nbsp;</p></td><td colspan="1" rowspan="1"><p>true</p></td></tr><tr><td colspan="1" rowspan="1"><p>false</p></td><td colspan="1" rowspan="1"><p>false</p></td><td colspan="1" rowspan="1"><p>&nbsp;</p></td><td colspan="1" rowspan="1"><p>false</p></td></tr></tbody></table>

**If any one condition is true, the result becomes true.**

```javascript
let isAdmin = false;
let isOwner = true;

if(isAdmin || isOwner){
    console.log("Access granted");
}
```

1.  **Logical NOT (!)**
    

The **NOT (!) operator reverses the Boolean value**. We can use this operator when we need to check the reversed or opposite value.

<table style="min-width: 50px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><td colspan="1" rowspan="1"><p><strong>A</strong></p></td><td colspan="1" rowspan="1"><p><strong>!A</strong></p></td></tr><tr><td colspan="1" rowspan="1"><p>true</p></td><td colspan="1" rowspan="1"><p>false</p></td></tr><tr><td colspan="1" rowspan="1"><p>false</p></td><td colspan="1" rowspan="1"><p>true</p></td></tr></tbody></table>

**It simply flips the value.**

```javascript
let isLoggedIn = false;

if(!isLoggedIn){
    console.log("Please log in");
}
```

> ## Assignment operators

Assignment operators are **\=, +=, -= .** This operator assigns the value, we use it in the process of declaring of variables.

```javascript
let name = ‘mehtab
let haveGirlfriend = false
```

Now assume we have a an `age = 18` , we need to increase or decrease it

We can say that

```javascript
age = age + 1  // age will be redeclare with adding one to it
```

And since programmer are so lazy they made a shorthand notation for it, now we can use

```javascript
age -= 2 // decrease by 2
age += 5 // increase by 5
```

 And we want to increase it by only one we can do it like this

```javascript
age++; // increased value for the next time we use it
age--; // decreased value for the next time we use it
```

> ## Conclusion

In this article, we explored what are operators and how they can be useful and also important while working with data. Operators allows developers to assign values, perform calculation etc.

We understood different types of operators like arithmetic, comparison, logical operators.

As we will move forward in our journey we will be using them for mathematical calculations and if we will practice it more, that will help us being more comfortable and confident .

* * *
