what does && return in javascript expression

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

Answers:

Answer

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

Answer

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.

Answer

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;
}
Answer

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.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.