Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
If a is not undefined and b is not undefined, return b. –  The New Idiot Jun 25 at 7:49
    
That code is functionally equivalent to if (this.grid) { return this.grid.getStore().sum('fees');} –  openorclose Jun 25 at 7:55
add comment

4 Answers

up vote 2 down vote accepted

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');

share|improve this answer
    
if grid does not exist will it return False or this.grid which is undefined –  user3646965 Jun 25 at 8:00
    
It will then return this.grid, which is undefined , a falsy value. –  Cerbrus Jun 25 at 8:01
    
ok thanks , now i got it –  user3646965 Jun 25 at 8:05
1  
and it will not even try to call this.grid.getStore().sum('fees');, thus avoiding errors –  Boris Jun 25 at 8:44
add comment

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, strings that contain falsy things like "false" or "0000" or even unreadable babble like function(){return null;}();

share|improve this answer
    
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 at 8:17
    
yes you're right, or if(!!this.grid). but i thought it would be more comprehensible. i'll edit –  Boris Jun 25 at 8:24
add comment

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;
}
share|improve this answer
    
3. if ais defined not truthy, or better if it is not undefined –  algorhythm Jun 25 at 7:55
    
@algorhythm "" && 2 === "" while obviously "" is defined. –  freakish Jun 25 at 7:57
1  
@freakish yes "" is a defined string... but it's empty... –  algorhythm Jun 25 at 7:58
    
"" is falsy, as are 0 and 'false' –  Boris Jun 25 at 7:58
    
@algorhythm Yes, that's exactly what I've written. Empty string is falsy. –  freakish Jun 25 at 7:59
show 5 more comments

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.