Categories:

JavaScript Kit > JavaScript Reference > JavaScript Operators: Comparison and Logic

Comparison and Logic Operators

Last updated: April 8th, 2008

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:
  • A Boolean when compared with a number results in the Boolean first being converted to a number (ie: true becomes 1)
  • A non string when compared with a string results in the non string first being converted to a string (ie: 5 becomes "5")

Note: null compared with undefined evaluates to true

All of the below return true:

1==true

"1"==true

45=="45"

null==undefined

=== (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: NaN strictly compared with NaN evaluates to false.

All of the below return false:

true===1

NaN===NaN

var x=[1,2]
var y=[1,2]
x===y //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:
  • A number compared with a string causes the string to be converted to a number first.
  • If one operand is an object, JavaScript will either try to convert it to a string (using toString()) or a number first depending on the other operand's type.
  • Date objects are compared numerically, with an earlier date (ie: yesterday) being smaller than a later date (ie: today).

These rules also apply to the other 3 comparison operators below.

x<y

var today=new Date()
var yesterday=new Date().setDate(today.getDate()-1)
alert(yesterday<today) //true

<= Less than or equal to. 5<=x
> Greater than. y>4

"15" > 3 //true, "15" changed to a number first

>= 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
x=(y<0)? 5: 10 //x contains 5

More example(s).

Logical Operators

Operator Description Examples
&& (AND) Logical AND used to compare 2 expressions (ie: exp1 && exp2). Some rules to remember:
  • If the first expression evaluates to null, 0, "", or undefined, then the value of that expression is returned.
  • If the first expression does NOT evaluate to null, 0, "", or undefined, then the value of the second expression is evaluated and returned.
  • false && anything always evaluates to false
0 && 25 //returns 0

"Me" && "You" //returns "You"

false && "not false" //returns false

true && false //returns false

|| (OR) Logical OR. Some rules to remember:
  • If the first expression evaluates to true, the value of that expression (unchanged) is returned.
  • If the first expression does NOT evaluate to true (ie: it's null or undefined), then the value of the second expression is evaluated and returned.
  • false || anything always evaluates to false
  • true || anything always evaluates to true
(1 || 25) //returns 1

var y=3
(x || y) //returns y, since x is undefined

! (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"))


Reference List

Right column

CopyRight (c) 2018 JavaScript Kit. NO PART may be reproduced without author's permission. Contact Info