# Operators in JavaScript

The most fundamental thing that makes programming so powerful is not just the ability to store data but to be able to perform some operations on it to modify the data based on some logic. Almost all data can have operations performed on it to modify its values. And to perform these operations we need some tools to define what kind of operation do we want to perform.

There are many different types of operators in JavaScript. All have their own use cases, compatible data types, syntax and rules. Javascript being a backward compatible language has some operators included with an improved version of a previous one, Based on what kind of task they do operators in JavaScript are divided into 4 main categories:

## 1\. Arithmetic operators

As the name suggests the arithmetic operators are used to perform operations on numbers. Even tho some of them can be used in other contexts too in general arithmetic operators represent the most fundamental mathematical operators in mathematics. Other complex operators exist but are mostly an extension of the basic ones which we will discuss here.

1.  **"+" :**  
    The plus or the addition operator is the fundamental operator used to perform addition. However it is an overloaded operator that can perform other tasks like string concatenation and implicit conversion of data types to make them compatible for an arithmetic addition or string concatenation.
    

```javascript
const brainrot = "pen" + "apple";
console.log(brainrot);
// prints penapple
const add = 2+5;
console.log(add);
// prints 7
const aBehavior = 2 + 'apples';
console.log(aBehavior);
// prints 2apples (converts 2 into string"
const addBool = 2+ true;
console.log(addBool);
//prints 3 (converts true into 1)
```

This shows how addition operator behaves in JavaScript. With more exploration this concept will become clearer. Also there is a special use case.

2.  **"-" :**  
    The minus or subtraction operator is used to perform arithmetic subtraction. It is also used to negate the value of an expression.  
    

```javascript
const subResult = 50 -5;
console.log(subResult);
//prints 45
const neg = -5;
console.log(neg);
//prints -5
console.log(2-'1')
//prints 1 (Performs implicit conversions)
```

3.  **"\*" :**  
    The multiplication operator is used to perform arithmetic multiplication in javascript. It is also used as \*\* for exponent.
    

```javascript
const a= 2*3;
console.log(a);
//prints 6
const exp = 2**3;
console.log(exp);
//prints 8
console.log(2*'5');
//prints 10 (performs implicit conversion) 
```

4.  **''/" :**  
    The division operator is used to perform arithmetic division.
    

```javascript
console.log(25/5);
//prints 5
```

It also performs implicit conversions and returns not an integer. On calculations like 5/0 it returns INFINITY and doesn't throw an error.

5.  **'%' :**  
    This operator is used to find the remainder.
    

```javascript
console.log(7%5);
//prints 2
```

##   
2\. Logical Operators

Logical operators are used to perform operations on boolean values. They evaluate the expression based on certain predefined criteria. Three of them are:

1.  **The Logical AND - && :**  
    The logical and operator returns true if both of the arguments are true and returns false in any other case. In fact if it detects the first statement to be false it will not even check for the other statement and declare false immediately.
    

```javascript
console.log( true && false);
//prints false;
console.log(false && true);
//prints false;
console.log(false && false);
//prints false;
console.log(true && true);
//prints true
```

2.  **The Logical OR - || :**  
    The logical or operator returns true if any one value is true and returns false if and only if both the values are false. It also returns true in case the first value is found to be true and skips checking the next value.
    

```javascript
console.log( true || false);
//prints true;
console.log(false || true);
//prints true;
console.log(false || false);
//prints false;
console.log(true |3| true);
//prints true
```

3.  **The Logical NOT - ! :**  
    The logical not operator just negates the results of the gven expression. Meaning it just returns true for false and false for true.
    

```javascript
console.log(!true);
//prints false
console.log(!false);
//prints true
```

## 3\. Comparison operators

Comparison operators are used to compare values based o equality, less , greater on non-equality. Each one having their own operator.

1.  **The "Less then and Greater Than" :**  
    The less than '<' operator returns true if the left side is less than the right side while the greater than '>' operator returns true if the left side is greater than the right side. Their usage can be interchanged by changing the position of the arguments. Because of course: 2< 3 is the same as 3> 2.
    

```javascript
console.log(3<2);
// prints false
console.log(2>1);
// prints true
```

2.  **The Equality and Non -Equality operators - introduction to strictness**
    

The equality and non-equality operators in JavaScript cannot be truly understood without understanding the concept of strictness in JavaScript. As we know JavaScript was a hasty language and has had many bugs due to its backward compatibility constraint.

When we would write something like: a==b or a!=b we would want it to give false if the values are unequal both in value and the type of value. Meaning that not only do we want 2==3 as false we also want 2=='2' as false too. But this actually doesn't play out like that in JavaScript.

The equality or non-equality operators would use to implicitly convert data into compatible types to perform equality based checks rendering 0=='0' as true. This caused problems as most logical code would want a stricter equality check. As such new extra = is added at the end of == or != to create this strictness by the new standard which allows for the desired behavior. Even tho this one too has some bugs in a few cases - it holds up pretty well to be used as the default comparison operator.

Hence even tho one can use:  

```javascript
console.log(3==2);
//or
console.log(5!=1)
```

for strictness it is always advisable to use:

```javascript
console.log(3===2);
//or
console.log(5!==1)
```

## 4\. Assignment operators

The assignment operators are used to assign values to variable. They work in the way such that a variable on the left hand side gets assigned the value on the right hand side of the operator. For example:

```javascript
const a = 10;
```

This means that a was assigned the value 20.

We have some special cases of assignment.

+= or -= etc (remember no space between the two symbols).

They are just a shorthand syntax to write an operation with the same variable.

```javascript
a = a+10;
//can also be written as 
a += 10;
```

In the the above case operators can be changed from + to '\*' or '/'. Hence they can be used to write short-hands for a= a\*10 or a = a-10.

Operators are the core of any language that helps us to write logic. Properly understanding the logic behind the operators and how to use them will always help us to write better code. Operators are the means to get closest to doing the same things as the CPU is doing inside. The basic operators implementation in those tiny chips have helped us to create this large computer empire. Never forget the power you can have in your hands by mastering operators.
