# Javascript operators

JavaScript operators include:

- Arithmetic Operators
- Assignment operator
- The comparison operator
- The ternary operator
- Logical operator
- String concatenation operator

operator | Description | Example | result |
---|---|---|---|

+ | Add | y=2 + 1 | y=3 |

- | Subtract | y=2-1 | y=1 |

* | multiplied | y=2 * 3 | y=6 |

/ | except that the result is a floating point type | y=6/3 | y=2 |

% | seeking more than the return of floating-point type requires two operands are integer | y=6% 4 | y=2 |

++ | Add, divided into before and after plus on the Boolean and NULL will be invalid | y=2 ++ y (before adding) y ++ (after adding) | y=3 |

- | Decrement, divided into pre-decrement and post-decrement On the Boolean and NULL will be invalid | y=2 - y (before subtraction) y - (after subtraction) | y=1 |

For the former plus after the increase, after the implementation of the results are variables plus 1, the difference is that the implementation of the results are not the same return, refer to the following two examples:

var x = 2; alert(++x); // Output : 3 alert(x); // Output : 3 var y = 2; alert(y++); // Output : 2 alert(y); // Output : 3

Decrease the same token.

## Assignment operator

Assignment operator = Assignment operator, the assignment operator is to assign the value to the right of the left variable. Set y = 6, see the table below:

operator | Example | Equivalent to | result |
---|---|---|---|

= | y=6 | y=6 | |

+= | y +=1 | y=y + 1 | y=7 |

-= | y -=1 | y=y-1 | y=5 |

*= | y *=2 | y=y * 2 | y=12 |

/= | y /= 2 | y=y /2 | y=3 |

%= | y%=4 | y=y% 4 | y=2 |

#### Assignment operations are nested

Assignment operators can be nested:

y = (x = 2) + 5; // Results : x=2，y=7

## The comparison operator

operator | Description | Example | result |
---|---|---|---|

== | equals | 2 == 3 | FALSE |

=== | is equal to (value and type must be compared) | 2 === 2 2 === "2" | TRUE FALSE |

!= | is not equal or can be written as < > | 2 == 3 | TRUE |

> | greater than | 2 > 3 | FALSE |

< | less than | 2 < 3 | TRUE |

>= | greater than or equal to | 2 >=3 | FALSE |

<= | less than or equal | 2 <=3 | TRUE |

The comparison operators can also be used for string comparisons.

## The ternary operator

Ternary can be regarded as a special comparison operator:

(expr1) ? (expr2) : (expr3)

Syntax Explanation: The value of expr2 is expr2 when expr1 evaluates to TRUE, or expr3 otherwise.

x = 2; y = (x == 2) ? x : 1; alert(y); // Output : 2

This example determines whether the value of x is equal to 2, and if x equals 2, the value of y is equal to x (equal to 2) and vice versa.

**Note**

In order to avoid errors, it is a good idea to enclose the expressions of the ternary operator in parentheses.

## Logical operator

operator | Description | Example | result |
---|---|---|---|

&& | Logical AND (and) | x=2; y=6; x && y > | FALSE |

|| | logical OR (or) | x=2; y=6; x && y > 5 | TRUE |

! | logical not, take the logical opposite | x=2; y=6; ! (X > y) | TRUE |

## String concatenation operator

The concatenation operator + is used primarily to connect two string or string variables. Therefore, when you use the operator on a string or string variable, you do not add them.

x = "Hello"; y = x + " world!"; // Results : y = "Hello world!" // In order to increase the space between the two strings, // you need to insert a space into a string: y = x + " world!"; // Results : y = "Hello world!"

When a join (addition) operation is performed on a string and a number, the number is converted to a string and then concatenated (summed):

x = 25; y = "I'm " + x + " years old"; // Results : y = "I'm 25 years old"