What does || mean? [duplicate]

若如初见. 提交于 2020-01-03 06:01:42

问题


Possible Duplicate:
What does this construct mean?

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?


回答1:


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.




回答2:


Crockford calls / called it a default operator




回答3:


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";



来源:https://stackoverflow.com/questions/7718259/what-does-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!