Comparison and Logic Operators
|==||Tests for equality in value between two
operands. Note that the two operands do NOT have to be of the same
performs conversion on a differing operand based on the following
|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.
|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.
|<=||Less than or equal to.||
|>=||Greater than or equal.||
|?:||The Conditional Operator (
(condition)? exp1 : exp2
|&& (AND)||Logical AND used to compare 2
||| (OR)||Logical OR. Some rules to remember:
|! (NOT)||Logical NOT.||
Example - Conditional Operator
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"))
- Global functions
- Escape Sequences
- Reserved Words
- Ajax (XMLHttpRequest)
- JSON object