- Assignment Operators
- Arithmetic Operators
- Numeric Assignment Operators
- Incremental/Decremental Operators
- Logical Operators
- Comparison Operators
- Typeof Operator

**Assignment Operators**Assignment operators enable us to assign values to variables for storage.

OPERATOR | EXAMPLE | EXPLANATION |
---|---|---|

= | x = 20 | Assigns 20 to the variable "x". |

+= | x += 30 | Assigns 30 in addition to what was previously stored on "x" and stores the new value on "x". |

var = var+value | x = x + 1 | Adds 1 to what was previously stored on "x" and stores the new value on "x". |

```
<script>
var str = "Hello";
str += " World";
alert(str);
</script>
```

**Arithmetic Operators**Arithmetic or mathematical operators enable us to perform arithmetic operations with numbers.

OPERATOR | NAME | EXAMPLE | EXPLANATION |
---|---|---|---|

+ | Addition Operator | 2 + 3 | Adds 2 to 3 and returns the new value which is 5. |

- | Subtraction Operator | 5 - 3 | Subtracts 3 from 5 and returns the new value which is 2. |

* | Multiplication Operator | 2 * 3 | Multiplies 2 by 3 and returns the new value which is 6. |

/ | Division Operator | 6 / 3 | Divides 6 by 3 and returns the new value which is 2. |

% | Modulus Operator | 3 % 2 | Divides 3 by 2 and returns the remainder which is 1. |

```
<script>
var x = 7;
var y = 5;
var z = x % y;
alert("The remainder when "+x+" ÷ "+y+" is "+z);
</script>
```

**Numeric Operators**Numeric operators allow us to carry out both arithmetic and assignment operations on numbers.

OPERATOR | EXAMPLE | SAME AS | EXPLANATION |
---|---|---|---|

+= | x += 5 | x = x + 5 | Adds 5 to whatever value that was previously stored on "x" and assigns the new value to x. |

-= | x -= 5 | x = x - 5 | Subtracts 5 from whatever value that was previously stored on "x" and assigns the new value to x. |

*= | x *= 5 | x = x + 5 | Multiplies 5 with whatever value that was previously stored on "x" and assigns the new value to x. |

/= | x /= 5 | x = x / 5 | Divides whatever value that was previously stored on "x" with 5 and assigns the new value to x. |

```
<script>
var x = 2;
x += 3; //adds 3 to what was previously on x, which is 2. x is now 5
alert(x);
</script>
```

**Incremental/Decremental Operators**Incremental or decremental operators allow us to increase and decrease numbers. They are mostly used with loops, which you will learn about in subsequent pages.

OPERATOR | NAME | EXAMPLE | EXPLANATION |
---|---|---|---|

variable++ | Post Incremental | x++ | Continuously adds 1 to the value of "x" after initial execution. |

++variable | Pre Incremental | ++x | Adds 1 to the value of "x" before initial execution and continues adding 1 for every execution. |

variable-- | Post Decremental | x-- | Continuously subtracts 1 from the value of "x" after initial execution. |

--variable | Pre Decremental | --x | Subtracts 1 from the value of "x" before initial execution and continues subtracting 1 for every execution. |

```
<script>
var x = 1;
while (x <= 3){
alert("Execution "+x+": x is "+x);
x++;
}
</script>
```

```
var y = 5;
y++; //5 - Post incremental
++y; //6 - Pre incremental
y--; //5 - Post decremental
--y; //4 - Pre decremental
```

**COMPARISON OPERATORS**Comparison operators enable us compare two values or variables.

OPERATOR | EXAMPLE | BOOLEAN RESULT | EXPLANATION |
---|---|---|---|

== | 5 == 6 | false | Compare 5 to 6 in order to know if both are of same values. |

!= | 5 != 6 | true | Compare 5 to 6 in order to know if both are not of same values. |

=== | 5 === "5" | false | Compares 5 to "5" in order to know if both are of same values and data type. |

!== | 5 !== "5" | true | Compares 5 to "5" in order to know if both are not of same values or data type. |

```
<script>
alert(5 === "5");
</script>
```

**LOGICAL OPERATORS**Logical operators enable us bring statements together to build stronger conditions.

OPERATOR | NAME | EXAMPLE | EXPLANATION |
---|---|---|---|

&& | ANDING Operator | 4 < 5 && 4 > 2 | Brings two conditions together i.e 4 < 5 and 4 > 2. The statement returns true if both statements are true. That is, if 4 is less than 5 and 4 is greater than 2. |

|| | ORING Operator | 4 < 5 || 4 > 2 | Brings two conditions together i.e 4 < 5 and 4 > 2. The statement returns true if any of the statements is true. That is, if 4 is less than 5 or 4 is greater than 2. It is also true if both are true. |

! | NOT Operator | !(4 > 2) | Returns true if 4 is not greater than 2 or the expression(4 > 2) is not true. |

```
<script>
var isLogged = false;
if (!isLogged)
{
alert("You are not logged in.");
}
else
{
alert("You are logged in.");
}
</script>
```

**Typeof Operator**The typeof() operator enables us know the data type of a value.

```
<script>
var x = 1;
alert(typeof(x));
</script>
```

```
<script>
var x = "Hello Beginner";
alert(typeof x);
</script>
```

**SUMMARY**

- Operators enable us perform operations on variables and values.
- JavaScript assignment operators enable us to assign values to declared variables.
- Arithmetic operators allow us perform mathematical operations.
- Numeric operators allow us perform both arithmetic and assignment operations. They carry out an arithmetic operation and assign the value gotten from that operation to a variable.
- Incremental/Decremental operators are used to increase or decrease a value outrightly or after an initial execution.
- Comparison operators enable us compare a value to another.
- Logical operators allow us build comprehensive logics using more than one statement.
- The
**typeof()**operator enables us know the data type of a value.