2
$('.blocks','#grid').each(function (i) {
              $(this).stop().css({opacity:0}).delay(100).animate({
                'opacity': 1
              }, {
                duration: 2000,
                complete: (i !== row * cols - 1) ||
                function () {
                  alert('done');
                }

              });
            });

What does the "||" operator mean in the "complete" property of the animate function?

Tamzin Blake
  • 2,594
  • 20
  • 36
hamahama
  • 393
  • 2
  • 5
  • 17
  • 2
    possible duplicate of [In Javascript, what does it mean when there is a logical operator in a variable declaration?](http://stackoverflow.com/questions/3088098/in-javascript-what-does-it-mean-when-there-is-a-logical-operator-in-a-variable-d) – Felix Kling Jun 22 '11 at 00:48
  • ah thanks i didn't know how to describe the situation so i couldnt get a good search result – hamahama Jun 22 '11 at 06:47

4 Answers4

2

It exploits short circuit evaluation. While the left hand side is truthy it won't bother evaluating the right hand side.

This is because with an OR, if one condition is truthy, it doesn't need to bother with additional conditions because it already knows enough information to answer the condition. They are also evaluated from left to right.

Also, in JavaScript, instead of the condition returning true or false, it returns the last operand it evaluated. You can see why this is useful.

alex
  • 479,566
  • 201
  • 878
  • 984
  • 1
    Whoever commented about *whilst*, I've placed a link to a mention on Wikipedia. – alex Jun 22 '11 at 00:54
  • I didn't comment on it, but basically all Wiki says is to use "while" instead of "whilst" to prevent sounding pretentious because whilst is not used in the vernacular anymore. – vol7ron Jun 24 '11 at 02:32
  • @vol7ron: Fair enough, I will change it ;) – alex Jun 24 '11 at 04:05
0

|| is a logical operator http://www.w3schools.com/js/js_comparisons.asp

it will 'favor' the left hand side until the left hand condition is not met anymore, at which point it will execute the right side, which is an anonymous function that executes an alert.

thescientist
  • 2,906
  • 1
  • 20
  • 15
0

It's the logical OR operator, but it only evaluates the right-hand expression if the left-hand expression is false.

If the left-hand expression is true then it knows that the whole expression evaluates to true, so it doesn't bother evaluating (or executing) the right-hand expression.

Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
0

The expression seems to be a fade in.

The logical OR || is similar to an else case of an if statement. If the left side evaluates to false, the function is created and an alert is called.


Given:

  • (i !== row * cols - 1) || function () { alert('done');  }
    

The function declaration would be assigned in this case:

  • false || function (){ alert('done'); }
    

Note, that "complete" property would not hold a value, just a function definition, you would need to call complete as something like complete(). For the function to be called if the left side is false, you'd have to surround it in parentheses and call it:

  • complete: (i !== row * cols - 1) || (function () {alert('done');})()
    

Note: The left hand side will only be false if something is undefined (i think), or if i turns out to equal rows*cols -1.

vol7ron
  • 40,809
  • 21
  • 119
  • 172
  • I agree with what you've said, but an added note: to my eye `complete` is a weird sort of declaration in that it may end up being either true (a non-function), or a reference to the alerting function. Can't say I've ever needed (or wanted) to do that in my own code - the nearest I've come is a choice of either null/undefined or a function reference. – nnnnnn Jun 22 '11 at 03:48
  • I'd say, a function is a value too ;) – Felix Kling Jun 22 '11 at 08:02
  • @nnnnn: to me it looks like it was put there for debugging, since it is using an `alert`; however the user may be later using the "complete" property as something like `if (this.complete != true){this.complete()}` -- notice that it has to be compared to true, since holding the function is truthy; in other words, you couldn't do `if (!this.complete){ ... }` because in either case it would result in a truthy value. You could abuse `NaN`, though, and do `if(!(a*1)){ ... }` – vol7ron Jun 22 '11 at 13:08
  • @Felix - I know (but I think you know what I meant). @vol7tron - yes, I thought of the usage with the if statement just as you describe, but it gives me a bad vibe. While I wouldn't say all unorthodox coding constructs are bad, I'm wary of "tricky" things that might make sense at the time I write them but will confuse if I have to debug the code a month later. – nnnnnn Jun 22 '11 at 23:27
  • @nnnnnn: that's a legitimate fear of new programmers. As you progress, you'll look for ways to shorten code and reduce processing time. It's a form of optimization that isn't required, but is coveted amongst advanced programmers. If something is tricky, it's worth putting in your documentation, which is permitted to be lengthy, but external to your code. – vol7ron Jun 23 '11 at 05:58
  • @vol7ron - I'm not a new programmer, and indeed if working on a project by myself I'd probably do some optimised things, but at work I prefer to avoid unorthodox ways of doing things so that I don't have to worry about other people on the team getting confused by something and breaking it (sadly I've found that leaving comments in the code isn't enough). The time I could save myself is outweighed by the time potentially lost later explaining stuff to the others. Also I don't tend to "pre-optimise", that is, I'll try to make the code clear and then optimise later if it performs badly. – nnnnnn Jun 23 '11 at 06:51
  • @n6: that's my point, though. it's not unorthodox the more you implement it and the more time you spend immersed in the language. I'm not saying you're a bad programmer, just that someone who does JS daily is looking more for reduced lines - the more lines you can read on a page (w/o unnecessary obfuscation), the more efficient you'll become when debugging, adding, or repairing. I agree, if this is a wide project, either make sure your peers have an advanced level of understanding, or don't unnecessarily use it. – vol7ron Jun 24 '11 at 02:26