What does || mean? [duplicate]
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";
精彩评论