Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...