Using logical operators (&& and OR) in javascript


Note: Current blog-post is old. You can find latest/new blog post here:

Logical Operators in Javascript, an in-depth overview

Logical AND operator (&&)

For logical AND operator (&&) both operands should be true. Otherwise result will be false.

  1: console.log( true && true ) // true
  2: console.log( false && true ) // false

Unlike other strongly type languages, both operands are not necessarily Boolean in javascript.

  1: console.log( 'hello' && false ) // false
  2: console.log( false && 'hello' ) // hello

If one operand is object and other is Boolean then second operand is returned, as shown above. The same rule applies if both operands are objects.

The value produces by && or || operator is not necessarily of type Boolean. The value produces will always be the value of two operand expressions.

If either operand is null, null operand is returned.

  1: console.log( null && false ) // null
  2: console.log( +'23AB' && false ) // NAN
  3: console.log( undefined && false ) // undefined
  4: console.log( +'23AB' && null ) // NAN
  5: console.log( undefined && +'23AB' ) // undefined

In javascript, logical AND operator (&&) can be said to short-circuited operator because if first operand is false, then javascript engine did not execute second operator and simply skip it because result always false.

  1: console.log( false && +'23AB' ) // false
  2: console.log( false && undefined ) // false
  3: console.log( false && null ) // false

Logical OR operator (||)

For logical OR operator (||) one operand should be true for true result. Otherwise result will be false.

  1: console.log( false || false ) // false
  2: console.log( true || false ) // true

Unlike other strongly type languages, both operands are not necessarily Boolean in javascript.

  1: console.log( 'hello' || false ) // hello
  2: console.log( true || 'hello' ) // true

If one operand is object and other is Boolean then first operand is returned, as shown above. The same rule applied if both operands are objects.

  1: console.log( false || null ) // null
  2: console.log( false || +'23AB') // NAN
  3: console.log( false || undefined ) // undefined
  4: console.log( null || +'23AB') // NAN
  5: console.log( +'23AB' || undefined ) // undefined

In javascript, logical OR operator (||) can be said to short-circuited operator because if first operand is true, then javascript engine did not execute second operator and simply skip it because result always true.

  1: console.log( true || +'23AB' ) // true
  2: console.log( true  || undefined ) // true
  3: console.log( true  || null ) // true

Comparing && and || operator

&&

||

If one operand is object and other is Boolean then second operand

is returned. The same rule applies if both operands are objects.

If one operand is object and other is Boolean then first operand

is returned. The same rule applied if both operands are objects.

For logical AND operator (&&) both operands should be true.

Otherwise result will be false.

For logical OR operator (||) one operand should be true for true

result. Otherwise result will be false.

If either operand is null, null operand is returned.

  1: console.log( false || null ) // null
  2: console.log( true || null ) // true

Note: I tested all above examples in Firebug 1.7x.0a11 extension/add-ons.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s