JavaScript operators are used to assign values, compare values, perform arithmetic operations, and so on.
Assignment operators
An assignment operator assigns a value to its left operand based on the value of its right operand. You might be familiar with notation such as “x = x + y”, however as lazy as developers are, we of course have a shorter way of writing such operations.
Comparison operator
Compares its operands and returns a boolean based on whether the comparison is true or false.
Often, if the two operands are not of the same type, (for example 2 + ‘1’, where one is a number and the other is a string) JavaScript attempts to convert one or both operands to an appropriate type in order to compare the two. This is the difference between using equal and strict equal.
JavaScript converts the string and the boolean to a number in order to be able to make the comparison, without you really knowing. If you use strict equal, it makes sure that it only returns true if the type of the value equal the type of the value in the comparison.
In order to prevent unexpected behavior, it’s best to always use strict equal.
Arithmetic operators
Takes numerical values as their operands and returns a single numerical value.
Unary operation: an operation with only one operator.
Binary operator: an operation that operates on two operands and manipulates them to return a result.
Logical operators
Logical operators return a boolean value. Besides true and false, there are truthy and falsy values:
Falsy values don’t necessarily have the value false, they simply evaluate to false. There are 6 falsy values:
All truthy values are values that aren’t falsy (that sounds very obvious, but there are too many values that are truthy. It’s much simpler to make sure that the value isn’t in the list of falsy values!).
Precedence and associativity
Now that you know more about operators, imagine the following scenario:
Operators have a certain precedence: operators with a higher precedence get called first, operators with a lower precedence get called last. It can also happen that all operators have the same precedence, in that case the associativity (the order in which the operators get called: either left-to-right or right-to-left) is important.
There is an entire table that describes which operators have the highest precedence, which can be found here.