JavaScript comparison and logical operators
Comparison and logical operators for testing true or false.
Comparison
In comparison logic statements used to measure variables or values are equal.
x = 5, the table below explains the comparison operators:
Operators | description | Compare | return value | Examples |
---|---|---|---|---|
== | equal | x == 8 | false | Examples >> |
x == 5 | true | Examples >> | ||
=== | Absolutely equal to (value and type are equal) | x === "5" | false | Examples >> |
x === 5 | true | Examples >> | ||
! = | not equal to | x! = 8 | true | Examples >> |
! == | Absolutely not equal to (or type a value not equal) | x! == "5" | true | Examples >> |
x! == 5 | false | Examples >> | ||
> | more than the | x> 8 | false | Examples >> |
< | Less than | x <8 | true | Examples >> |
> = | greater than or equal to | x> = 8 | false | Examples >> |
<= | less than or equal to | x <= 8 | true | Examples >> |
how to use
You can use comparison operators in conditional statements to compare values, and then to take action based on the results:
You will learn more about conditional statements in the next section of this tutorial.
Logical Operators
Logical operators used to determine the logic between variables or values.
Given x = 6 and y = 3, the table below explains the logical operators:
运算符 | 描述 | 例子 |
---|---|---|
&& | and | (x < 10 && y > 1) 为 true |
|| | or | (x==5 || y==5) 为 false |
! | not | !(x==y) 为 true |
Conditional operator
JavaScript also contains certain conditions based on the variable conditions of the assignment operator.
grammar
example
Examples
If the variable age is less than 18, then assigned to the variable voteable "too young", otherwise assign "Age has been reached."