Like Java, JavaScript follows a similar kind of operators. You can read more about the operators in JavaScript from MDN documentation here.
Arithmetic operators are used to perform arithmetic operations such as addition, subtraction, multiplication and division.
Example:
var a = 1; var b = 2; console.log(a + b);
Output:
3
Relational operators are used to check if the relation that has been specified between the operands is right or wrong and this is why, the result of relational operators is not a number, but instead, either true or false.
Example:
var a = 1; var b = 2; console.log(a > b);
Output:
false
Logical operators take boolean values and return a boolean value as a result. You can also give expressions but these expressions must evaluate to either true or false.
The three most commonly used logical operators are AND (&&), OR (||) and NOT (!).
An AND operator between two boolean values(A and B) indicates that the result of A && B will be true only if both A and B are true. If either of them is false, then the result of A && B will be false.
An OR operator between two boolean values(A and B) indicates that the result of A || B will be true if at least one of A and B is true. If both of them are false, then the result of A || B will be false.
Here’s a truth table to understand the logical operations better.
A | B | A && B | A || B |
false | false | false | false |
true | false | false | true |
false | true | false | true |
true | true | true | true |
Example:
var x = true; var y = false; console.log(x && y);
Output:
false
The equality operator which is represented by == checks if two values are equal or not. This is different from the assignment operator = which is used to assign a value from the right-hand side to the variable on the left-hand side.
Example:
var a = 1; // assignment operator used var b = 2; // assignment operator used console.log(a == b); // equality operator used
Output:
false