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
Logical AND (
&&
):
expr1 && expr2
Returnsexpr1
if it can be converted to false; otherwise, returnsexpr2
. Thus, when used with Boolean values,&&
returnstrue
if both operands aretrue
; otherwise, returnsfalse
.
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');
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.
I'll start off with mentioning I'm very new to Angular so I'm probably not doing things in the correct "Angular way". I have the following ng-repeat looping through some data - the correct data is ...
I'll start off with mentioning I'm very new to Angular so I'm probably not doing things in the correct "Angular way". I have the following ng-repeat looping through some data - the correct data is ...
Having a div element, I can style its bg-color through either Javascript or CSS. Is there any difference between the two, or which one should we use? div:hover {background-color:gray;} or $( div)....
Having a div element, I can style its bg-color through either Javascript or CSS. Is there any difference between the two, or which one should we use? div:hover {background-color:gray;} or $( div)....
I need to extract the Operating System's name and the browser's name from the user agent string. Sample of user agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.9) Gecko/20100825 Ubuntu/9.10 (...
I need to extract the Operating System's name and the browser's name from the user agent string. Sample of user agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.9) Gecko/20100825 Ubuntu/9.10 (...
I am trying to put together a responsive site that works on a desktop/ipad/iphone/android tablet etc. I am trying to add in the facebook style left hand menu - the one that has the three lines that ...
I am trying to put together a responsive site that works on a desktop/ipad/iphone/android tablet etc. I am trying to add in the facebook style left hand menu - the one that has the three lines that ...