3

I was reading the javascipt code in some application and code was this

getTotalFees:function(){
        return this.grid
        &&this.grid.getStore().sum('fees');
}

Now i am confused what it will return.

IT looks to me like

return a&&b

won't it return true or false rather than b

Mirage
  • 30,868
  • 62
  • 166
  • 261

4 Answers4

3

Logical AND (&&):
expr1 && expr2 Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.

Source

So, basically:
If the first parameter is falsy, it returns that parameter. Else, it literally returns the second parameter.

In your case, this means that, if this.grid exists, it returns this.grid.getStore().sum('fees');

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • if grid does not exist will it return `False` or `this.grid` which is `undefined` – Mirage Jun 25 '14 at 08:00
  • It will then return `this.grid`, which is `undefined `, a [`falsy`](http://www.sitepoint.com/javascript-truthy-falsy/) value. – Cerbrus Jun 25 '14 at 08:01
  • 1
    and it will not even try to call `this.grid.getStore().sum('fees');`, thus avoiding errors – Boris Jun 25 '14 at 08:44
1

You misunderstand what && does. Let a and b be "entities". Then a && b does:

  1. evaluate a
  2. if a is falsy return a
  3. if a is truthy evaluate b
  4. return b

Example:

var f = function() {
    console.log("test");
    return 'foo';
}

> 0 && f()
0
> 1 && f()
test
"foo"

Note that in first case we didn't get console.log because f() was not evaluated because 0 is falsy. This property is important and actually

a && b != b && a

even though mathematically it should be the same (but it is not due to side-effects of evaluation).

Falsy values include: 0, false, "" (empty string), null, undefined,NaN (not a number type). I don't think there are any other possible values (someone correct me if I'm wrong). Every other object is truthy.

So in your case the code can be rewritten as:

if (this.grid) {
    return this.grid.getStore().sum('fees');
} else {
    return this.grid;
}
freakish
  • 54,167
  • 9
  • 132
  • 169
1

This is done to protect against calling a method on undefined property, witch would cause an error. So if this.grid is undefined, then undefined is returned.

In expressions if a && b when a equals to false (or in javascript it can be an expression like in Cerburs answer), then a is returned.

Similarly with || operator, the first from the left that equals to true (in javascript not 0, not undefined, not null, not NaN, and not false of course) is returned.

Harazi
  • 122
  • 1
  • 10
0

Ok, let's assume that this.grid.getStore().sum('fees') returns something, let's say "okay!".

now the return statement in your code is a convoluted way of saying :

if(this.grid)//this.grid is defined and doesn't evaluate as 'false'
    return this.grid.getStore().sum('fees');
else
    return this.grid;

if this hasn't got a grid, we return undefined, else we call gridStore... and return its own return. It is a common way of avoiding "undefined has no method 'gridStore'"

the VERY important part is that, in a && f(), f() will NOT be called if 'a' evaluates to false. there are many things that evaluate to false, such as any undefined variable, null, empty strings... (note that strings that contain falsy things like "false" or "0000" are actually truthy). or even unreadable babble like function(){return null;}(); may evaluate as false.

Boris
  • 1,161
  • 9
  • 20
  • Actually that's not the same code. The `if` statement should be `if (this.grid)`. Otherwise it does different thing (what if `this.grid` is `0`?). – freakish Jun 25 '14 at 08:17
  • yes you're right, or `if(!!this.grid)`. but i thought it would be more comprehensible. i'll edit – Boris Jun 25 '14 at 08:24