### Operators in JavaScript

Operators in JavaScript is a function or a symbol which tales compiler to perform action subsequent operands.

For example

```4 + 5
```

In the above example + is an operator 4 and 5 are operands so, compiler tales computer to do the addition of 4 and 5.

JS supports 5 types of operators:

• Arithmetic Operators
• Assignment Operators
• Comparison Operators
• Logical/Relational Operators
• Conditional/Ternary Operators

## Arithmetic Operators In JavaScript

Arithmetic Operators are the symbols that represent arithmetic operations such as addition (+), Subtraction (-), etc

### Types Of Arithmetic Operators

1. Addition (+): Used to add two numbers
2. Subtraction (-): Used to subtract two numbers (-)
3. Multiplication (*): Multiplies both operands
4. Division (/): Divide numerator by denominator
5. Modulus (%): Gives the remainder of integer division
6. Increment (++): Increases an integer by one
7. Decrement (–): Decreases an integer by one

### Example of Arithmetic Operators

```<!DOCTYPE html>
<html>

<head>
<title>Arithmetic Operators In JavaScript</title>
</head>

<body>
<h3>Arithmetic Operators In JavaScript</h3>
<script>
var a = 15;
var b = 7;
document.write("a + b =>" + (a + b) + "</br>"); //Addition
document.write("a - b =>" + (a - b) + "</br>"); //Subtraction
document.write("a * b =>" + (a * b) + "</br>"); //multiplication
document.write("a / b =>" + (a / b) + "</br>"); //division
document.write("a % b =>" + (a % b) + "</br>"); //modulus
document.write("a++ =>" + (++a) + "</br>");   //increment
document.write("b-- =>" + (--b) + "</br>");   //decrement
</script>
</body>

</html>
```

### Addition for Strings (+)

This operator is also used for the addition of two strings and string and number.

```<!DOCTYPE html>
<html>

<head>
<title>Addition of STRINGS in JavaScript</title>
</head>

<body>
<h3>Addition of STRINGS in JavaScript</h3>
<script>
var a = 5;
var b = " Integer Addition "
var c = a + b;
var d = "integer" + " " + "addition";
var e = a + "5";
document.write(a);
document.write("</br>");  //to break the line
document.write(b);
document.write("</br>");
document.write(c);
document.write("</br>");
document.write(d);
document.write("</br>");
document.write(e);
</script>
</body>

</html>
```

## Assignment Operators In JavaScript

The assignment operators is used to assign a value to a variable.

### There are 6 types of assignment operator

1. Simple Assignment (=): It assigns a value from left to right.
2. Add and assignment (+=): It does the addition of the right operand with the left operand and assigns the result to the left operand.
3. Subtract and assignment operator: It subtracts the right operand from the left operand and assigns the result to the left operand.
4. Multiply and assignment (*=): It multiplies the right operand with the left operand and assigns the result to the left operand.
5. Divide and Assignment (/=): It divides the left operand with the right operand and assigns the result to the left operand.
6. Modulus and Assignment (%=): It divides the left operand from the right operand and assigns a reminder to the left operand.

### Example Of Assignment Operators

```<!DOCTYPE html>
<html>

<head>
<title>Assignment Operators In JavaScript</title>
</head>

<body>
<h3>Assignment Operators In JavaScript</h3>
<script>
var a = 15;   //Simple Assignment
var b = 7;
document.write("a += b =>");  //addition and assignment
a += b;
document.write(a + "</br>");
document.write("a -= b =>");  //Subtraction and assignment
a -= b;
document.write(a + "</br>");
document.write("a *= b =>");    //Multiplication and assignment
a *= b;
document.write(a + "</br>");
document.write("a /= b =>");  //divide and assignment
a /= b;
document.write(a + "</br>");
document.write("a %= b =>");  //modulus and assignment
a %= b;
document.write(a + "</br>");
</script>
</body>

</html>
```

### Comparison Operators In JavaScript

This operator compares both operand with each other and return the Boolean value true or false.

There are 6 types of comparison operator:

1. Is Equal (==): This operator is used to check if the right operand is equal to the left operand if it is then return true else return false.
2. Not equal (! =): This operator is used to check if the left operand is equal to the right operand, if it then returns false else return true. It is the opposite of Is Equal operator.
3. Greater than (>): This operator checks if the left operand is greater than the right operand if it is then returns true else return false.
4. Less than (<): This operator checks if the left operand is less than the right operand if it is then return true else return false.
5. Greater than or equal to operator (>=): This operator checks if the right operand is greater than or equal to the left operand, if it is then return true else return false.
6. Less than or equal to operator (<=): This operator checks if the right operand is less than or equal to the right operand, if it is then return true else return false.

### Example Of Comparison Operators

```<!DOCTYPE html>
<html>

<head>
<title>Comparison Operators In JavaScript</title>
</head>

<body>
<h3>Comparison Operators In JavaScript</h3>
<script>
var a = 15;
var b = 7;
document.write("(a==b)=>" + (a == b) + "</br>");   //Is equal to
document.write("(a!=b)=>" + (a != b) + "</br>");   //Not equal to
document.write("(a>b)=>" + (a > b) + "</br>");     //greater than
document.write("(a < b)=>" + (a < b) + "</br>");   //less than
document.write("(a>=b)=>" + (a >= b) + "</br>");   //greater than or equal to
document.write("(a<=b)=>" + (a <= b) + "</br>");   //less than or equal to
</script>
</body>

</html>
```

## Logical Operators In JavaScript

It is an operator that connects two or more expressions such that the result of that compound expression depends on both the individual expression and the operator being used.

Example:

If Ram waited for the bus. The bus was late. So, Ram will also get late.

In the above example, Ram’s timing depends on the timing of the bus thus both statements affect the final result.

### JavaScript has 3 types of Logical Operators

#### 1.Logical AND (&&)

If both expressions are true then, it returns true else returns false.

Truth table:

#### 2.Logical OR (||)

If any of the expression is true then it returns true else returns false.

Truth table:

#### 3.Logical NOT (!)

If returns false for true statements and true for false statements.

Truth table:

### Example Of Logical Operators

```<!DOCTYPE html>
<html>

<head>
<title>Logical Operators In JavaScript</title>
</head>

<body>
<h3>Logical Operators In JavaScript</h3>
<script>
var a = 11;
var b = 4;
//logical AND
document.write("Logical AND </br>" + (a > 12 &amp;&amp; b > 7) + "</br>" + (a < 12 &amp;&amp; b > 7) + "</br>" +
(a > 12 &amp;&amp; b < 7) + "</br>" + (a < 12 &amp;&amp; b < 7) + "</br> Logical OR </br>");

//logical OR
document.write((a > 12 || b > 7) + "</br>" + (a < 12 || b > 7) + "</br>" +

(a > 12 || b < 7) + "</br>" + (a < 12 || b < 7) + "</br> Logical NOT </br>");

//logical NOT
document.write((!(a == b)) + "</br>" + (!(a < 12)));
</script>
</body>

</html>
```

### Bitwise Operators In JavaScript

This operator is used to change individual bit in an operand.

1. Bitwise AND (&): It does a Boolean AND on each bit of its integer arguments.
2. Bitwise OR (|): It does a Boolean OR on each bit of its arguments.
3. Bitwise XOR (^): It does Boolean exclusive OR on each bit which means either one of them can be true not both.
4. Bitwise NOT (~): It is a unary operator that reverses all the bits of operand.

### Example Of Bitwise Operators

```<!DOCTYPE html>
<html>
<head>
<title>Bitwise Operators In JavaScript</title>
</head>
<body>
<h3>Bitwise Operators In JavaScript</h3>
<script>
var a = 2;
var b = 3;
document.write((a &amp; b) + "</br>");  //Bitwise AND
document.write((a | b) + "</br>");  //Bitwise OR
document.write((a ^ b) + "</br>");  //Bitwise XOR
document.write((~a) + "</br>");     //Bitwise NOT
</script>
</body>
</html>
```

## Conditional Operator In JavaScript

This is operator first check that if a given condition is true or false and then performs tasks based on the result.

### Example Of Conditional Operator

```<!DOCTYPE html>
<html>

<head>
<title>Conditional operator In JavaScript</title>
</head>

<body>
<h3>Conditional operator In JavaScript</h3>
<script>
var a = 2;
var b = 3; document.write((a < b) ? "True" : "False");
document.write("</br>");
document.write((a > b) ? "True" : "False");
</script>
</body>

</html>
```
Do you know? This article is contribute by Rushikesh Tayade If you want share your knowledge on Study4Geeks then go to login section.