Comparison and logical operators are used to test true or false.
JavaScript comparison and logical operators
Comparison and logical operators are used to test true or false.
Comparison Operators
Comparison operators are used in logical statements to determine whether variables or values are equal.
Operator
Description
Comparison
Return Value
Instance
= X = 8 false instance»
X = 5 true instance»
=== Absolutely equal to (Value and type are equal) x === "5" false instance»
X = 5 true instance»
! = Not equal to x! = 8 true instances»
! = Definitely not equal to (values and types are not equal) x! = "5" true instance»
X! = 5 false instances»
> Greater than x> 8 false instances»
<Less than x <8 true instance»
> = Greater than or equal to x> = 8 false instances»
<= Less than or equal to x <= 8 true instance»
How to Use
You can use a comparison operator in a condition statement to compare values and then take actions based on the results:
if (age<18) x="Too young";
In the next section of this tutorial, you will learn more about conditional statements.
Logical operators
Logical operators are used to determine the logic between variables or values.
Given x = 6 and y = 3, the following table describes the logical operators:
Operator
Description
Example
& And (x <10 & y> 1) is true
| Or (x = 5 | y = 5) is false
! Not! (X = y) is true
Conditional Operators
JavaScript also contains conditional operators that assign values to variables based on certain conditions.
Syntax
variablename=(condition)?value1:value2
Example
Instance
If the value of age is less than 18, the voteable variable is assigned "the age is too small"; otherwise, the value "The age has reached ".
Voteable = (age <18 )? "The age is too small": "The Age has reached ";
The above is the content of JavaScript comparison and logical operators in the [JavaScript tutorial]. For more information, see PHP Chinese website (www.php1.cn )!