24
Recently reading the plugin code, I noticed the definition of variables and objects using the operator || of Javascript. Example:
function ola(nome){
nome = nome || "estranho";
return "Olá, " + nome;
}
I’ve never seen the operator || be used outside a if(), then my question is, under what circumstances does the "or" operator work when defining variables? How do I know what would return after the "or"?
Note: Most of the answers below are correct and bring details that complement each other. As only one can be chosen as "correct" I chose the one that brought, in addition to the explanation, a list with what is considered false or true by the operator
||, but I highly recommend reading the other answers.
Including, not only the
||can be used out of aif, but also the logical operator&&: What is the && in between strings operator?– Miguel Angelo