-1

I find it in other people's source code alot. for Example

 var y = $('some-selector'),
            u = $(document).scrollTop();
        0 > u && (u = 0); //mysterious line..
var w = 'another variable';

Here's another example :

! function(foo) {
    foo.fn.equalWidths = function() {
        var u = 0,
            t = foo(this);
        return t.each(function() {
            var c = foo(this).innerWidth();
            t > u && (u = t)  // mysterious line again
        }), t.css("width", u)
    }, foo("[data-equal]").each(function() {
        var u = foo(this),
            t = u.data("equal");
        u.find(t).equalWidth()
    })
}(jQuery);

Please what's the exact function of the mysterious lines from the codes above? What does the comparison do..? is it a short form of something..?

SourceCode
  • 17
  • 6
  • Reading [the documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators) might be helpful. – Teemu Sep 07 '15 at 05:18
  • 1
    possible duplicate of [Javascript AND operator with assignment](http://stackoverflow.com/questions/3163407/javascript-and-operator-with-assignment) – Sebastian Simon Sep 07 '15 at 05:19
  • @SourceCode The `&&` basically evaluates all expressions and statements until it hits the first falsy value, whereupon that value is returned as the result of the entire expression. If there is no falsy value, it returns the last value which is truthy. For example `2 && 0 && a=1` returns `0` and doesn’t assign `1` to `a`; `2 && 3 && a=1` on the other hand returns `1` and _does_ assign `1` to `a`. – Sebastian Simon Sep 07 '15 at 05:34

1 Answers1

0

The && is the logical AND. It is normally used to check if two statements are true. If the first statement is false, there is no need to evaluate the second half, because you already know that at least one of the statements is false. Here, it is being used as an if statement, as Tushar has pointed out.

waternova
  • 1,472
  • 3
  • 16
  • 22