JavaScript Kit > JavaScript Reference > JavaScript Operators: Comparison and Logic
Comparison and Logic Operators
Below lists the operators in JavaScript related to comparison and logic.
Also See: Comparison and Logic Operators precedence and associativity.
Comparison Operators
Operator | Description | Examples |
---|---|---|
== | Tests for equality in value between two
operands. Note that the two operands do NOT have to be of the same
type to yield true for equality, as JavaScript automatically
performs conversion on a differing operand based on the following
rules:
Note: |
All of the below return true:
|
=== (strict equality) | Performs a strict test for equality between two operands
for both type and value. No automatic conversion is performed, so a
strict comparison only evaluates to true if the two operands are
identical in both type and value. This means, for example, a form
field that contains a number value will always return true with
strictly compared with an actual number of the same value, as the
former is a string.
When comparing two object references using strict equality, they are equal only if they refer to the same object. Two objects with identical methods and properties will return false. Note: |
All of the below return false:
|
!= | Tests for inequality in value between two operands. Type conversion is performed using same rules as equality operator. | |
!== (strict inequality) | Tests for strict inequality in value between two operands. No type conversion is performed. | |
< | Less than. Some rules to remember:
These rules also apply to the other 3 comparison operators below. |
x<y
|
<= | Less than or equal to. | 5<=x |
> | Greater than. | y>4
|
>= | Greater than or equal. | x>=y |
Conditional Operator
Operator | Description |
---|---|
?: | The Conditional Operator (?: )
is a shorthand method for returning the result of either exp1
or exp2, depending on whether condition evaluates to
true or false. If true, the value of exp1 is returned,
or if false, the value of exp2 instead.(condition)? exp1 : exp2 For example: y=-1 |
Logical Operators
Operator | Description | Examples |
---|---|---|
&& (AND) | Logical AND used to compare 2
expressions (ie: exp1 && exp2 ). Some rules to remember:
|
0 && 25 //returns 0
|
|| (OR) | Logical OR. Some rules to remember:
|
(1 || 25) //returns 1
|
! (NOT) | Logical NOT. | !(x>3) |
Example - Conditional Operator
The Conditional Operator is handy for quickly assigning a different value to a variable depending on the outcome of a condition. Here are a few examples:
var browser=(navigator.appName.indexOf("Microsoft")!=-1)? "IE" : "Non IE"
You can expand the number of possible values to assign to more than just two. In fact, there is no limit. Observe the below example, which has 3 possible values:
var browser=(navigator.appName.indexOf("Microsoft")!=-1)? "IE" : (navigator.appName.indexOf("Netscape")? "NS" : "Not IE nor NS"
In the 2nd example, "browser" will contain one of 3 possible values depending on the browser maker of the visitor.You can use a conditional operator within a larger expression, by wrapping the entire conditional operator with a parenthesis:
document.write("The total for your order is "+(onsale==true? "$25" : "$30"))
- JavaScript Operators
- JavaScript Statements
- Global functions
- JavaScript Events
- Escape Sequences
- Reserved Words