开发者

What does || mean? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

What does this construct mean开发者_JAVA技巧?

I'm encountering this syntax for the first time and am not sure what it's doing:

self.name = _searchString(settings.dataBrowser) || "An unknown browser";

What does the or (double pipes) condition do? When would self.name be set to the second value?


This is the logical or operator.

It evaluates to its first "truthy" operand.

In particular, it will evaluate to the second operand if the first operand is "falsy" — null, false, undefined, 0, "", or NaN.


Crockford calls / called it a default operator


this is directly related to a question i have asked, you can read about it here Short-circuit evaluation via the AND operator in PHP

so basically, it sets self.name to the value returned from the function, but if the function returns false, it sets itself to "An unknown browser";

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜