# Javascript operators examples

Learn how to use operators in javscript, Assignment operator, Comparison operator, Logical operator, Bitwise operator, Arithmetic operator in Javascript.

## JavaScript Assignment Operators

Assignment operators are used when we want to assign some value to a variable.

At the time of assignment, we can use the current value and then increment or decrement the assigned value.

• `=`
assigning the right side value to variable in left side
a = b, means assigning the value of b to a
• `+=`
current value + new value
var a = 10; a += 5;
that means a will become a+5= 15
• `-=`
current value - new value
var a = 10; a -= 5;
that means a will become a-5= 5
• `*=`
current value * new value
var a = 10; a *= 5;
that means a will become a*5= 50
• `/=`
current value / new value
var a = 10; a /= 5;
that means a will become a/5= 2

### JavaScript Comparison Operators

Comparison operators are used when we want to compare two variable, if they are equal, not equal, greater and equal, less than equal etc.

• `==`
equal to, check if both equal, Returns true or false
a==b, means a equal to b
• `!=`
Not equal to, check if both not equal, Returns true or false
a!=b, means a not equal to b
• `>`
greater than, Returns true or false
a>b, means a greater than b
• `>=`
greater than equal to, Returns true or false
a>=b, means a greater than equal to b
• `<`
less than, Returns true or false
a<b, means a less than b
• `<=`
less than equal to, Returns true or false
a<b, means a less than equal to b
• `===`
When we want to compare both conditions, equal value and equal type, we use `===` operator.
```let a=10;b=10;
if(a===b)
console.log("true");
else
console.log("false");
```

### JavaScript Logical Operators

Logical operators are basically when we want to apply logic to the line of code using some operator, are called logical operators, there are only three!

• `||`
OR operator
(a === 10 || b === 5) is false
• `&&`
AND operator
(a < 10 && b >5) is true
• `!`
NOT operator
!(a == b) is true

### JavaScript Bitwise Operators

• `~`
NOT
a = ~ 5
```console.log(~3); // -4
console.log(~5); // -6
console.log(~8); // -9
```
• `^`
XOR
XOR ^ : Sets each bit to 1 if only one of two bits is 1 `a = 5 ^ 1`
```console.log(3^2); // 1
console.log(2^2); // 0
console.log(3^3); // 0
console.log(10^5); // 15
console.log(10^2); // 8
console.log(5^1); // 4
```

### JavaScript Arithmetic Operators

Here we use two variable `(a=10, b=5)` to show you the examples below.

• `%`
Modulus (division remainder) operator
```let a = 10; b = 5;
r1 = eval(a % b);
console.log(r1);
```
result zero
• `++`
Increment operator
```var a = 10; b = 5;

r1 = a+a;
console.log(r1); // 20

a++;
r2 = a; // 11
console.log(r2);
```
• `--`
Decrement operator example
```var a = 10; b = 5;
r1 = a-a;
console.log(r1); // 0

a--;
r2 = a; // 9
console.log(r2);
```
• `+`
```let a = 10; b = 5;
r1 = eval(a + b);
console.log(r1);
```
• `-`
Subtraction operator
```let a = 10; b = 5;
r1 = eval(a - b);
console.log(r1);
```
result 5
• `*`
Multiplication operator
```let a = 10; b = 5;
r1 = eval(a * b);
console.log(r1);
```
result 50
• `/`
Division operator
```let a = 10; b = 5;
r1 = eval(a / b);
console.log(r1);
```
result 2