15

I am using a large JS library to perform certain drawing operations in canvas. Reviewing the library code (to make accommodating changes), I have ran into the '||' operator being used in a fashion which certainly should not evaluate to Boolean. Does this mean that this is a different operator or am I missing something obvious? An example follows:

var $time = Date.now || function(){
return +new Date;
};
Kara
  • 6,115
  • 16
  • 50
  • 57
KJ Saxena
  • 21,452
  • 24
  • 81
  • 109
  • I'm guessing it would be if Date.now for some reason returned a 0, then you would instead set $time to the return value of the function...? – peirix Sep 04 '09 at 11:16
  • @peirix: You guessed wrong. $time will be a function returning the current Date. It is set to be an alias of Date.now if it is defined (non-false) or the supplied function otherwise. – Ferdinand Beyer Sep 04 '09 at 11:19
  • Ah, that's why I didn't set it as an answer. No way of downvoting a comment :p So one would have to know more about the `Date.now` variable to know fully how this would work, then? – peirix Sep 04 '09 at 11:21

5 Answers5

17

There is already an accepted answer, but I like to mention, that the OR-Operator is also called Default-Operator, because it doesn't return a boolean, but instead the left or right hand argument.

Same goes for the AND-Operator, which is also called guard-Operator.

Check out crockford's Survey of the JavaScript Programming Language for more details:

The && operator is commonly called logical and. It can also be called guard. If the first operand is false, null, undefined, "" (the empty string), or the number 0 then it returns the first operand. Otherwise, it returns the second operand. This provides a convenient way to write a null-check:

var value = p && p.name; /* The name value will only be retrieved from p if p has a value, avoiding an error. */

The || operator is commonly called logical or. It can also be called default. If the first operand is false, null, undefined, "" (the empty string), or the number 0, then it returns the second operand. Otherwise, it returns the first operand. This provides a convenient way to specify default values:

value = v || 10; /* Use the value of v, but if v doesn't have a value, use 10 instead. */

Tim Büthe
  • 62,884
  • 17
  • 92
  • 129
13

The || operator evaluates to the first operand if it can be converted to true or the last operand otherwise. So in your example $time will be Date.now if it exists or the declared function otherwise.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
0

This is very common is javascript. If Data.now evaluates to true, then $time is set to that, else it is set to the function.

googletorp
  • 33,075
  • 15
  • 67
  • 82
  • The function doesn't have () attached to the closing brace, so $time is set to the function, not it's output. – Quentin Sep 04 '09 at 11:21
0

I believe that code is saying if the DateTime.now variable does not exist, instead return the result of +new Date.

Noon Silk
  • 54,084
  • 6
  • 88
  • 105
0

The || operator is being used to assign a value to $time in the example.

If Date.now evaluates to false, then $time is assigned the value on the right side of the || operator (in this case, a function). If Date.now evaluates to true, then it short-cicuits and assigns the value of Date.now to $time

Russ Cam
  • 124,184
  • 33
  • 204
  • 266