Javascript Operators can be categorized as:
- Comparison operators
- Conditional Operators
- Logical Operators
All the above three return true/false value.
Comparison Operator
Comparison operators are also known as relational operators. They are used in conditional statements, especially in loops where result of comparison decides whether execution should proceed. Comparison operators are used to test true or false. They are also used in logical statements to determine difference between values. Comparison operator cannot be used directly to compare objects .It can be used to compare only references of the object not the data they contain. It can be overloaded in user defined types. If == is overloaded, !== must be overloaded and same rule is applied to pairs and =. The following table shows the comparison operators:
Operator | Description |
---|---|
Equal(==) | It returns true if two operands are equal otherwise false. |
Strict equal(===) | it returns true if operands are exactly equal. |
Not equal(!=) | It returns true if the operands are not equal. |
Strict not equal(!==) | returns true if the operands are not equal or not of same type. |
Greater than(>) | It returns true if left operand is greater than right operand. |
Greater than or equal(>=) | It returns true if left operand is greater than or equal to right operand. |
Less than(<) | It returns true if left operand is less than right operand. |
Less than or equal(<=) | It returns true if left operand is less than or equal to right operand. |
Comparison operators can also be used in conditional statements to compare values as follows: Example:
If(age<20) b=”younger”;
Conditional Operator
It is also known as Ternary operator. It assigns value to a variable based on some condition. It has following syntax:
variablename= (condition)? expression1:expression2
It contains following parameters. condition is boolean expression, expression1 is returned if condition is true and expression2 is returned if condition is false. Example:
a=(age<20)?”younger”:”older”;
Logical Operator
Logical operators are used with boolean values which determines logic between variables.
Operator | Desscription | Example |
---|---|---|
AND (&&) | It returns expr1 if it can be converted to false ,otherwise returns expr2. | expr1 && expr2 |
OR (||) | It returns expr1 if it can be converted to true, otherwise returns expr2. | expr1 || expr2 |
NOT (!) | It returns false if single operand can be converted to true, otherwise returns true. | expr1 && expr2 |