Recently reading the code for a plugin , I noticed the definition of variables and objects using the ||
JavaScript operator. Example:
function ola(nome){
nome = nome || "estranho";
return "Olá, " + nome;
}
I've never seen the ||
operator be used outside a if()
, so 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 provide details that are complete. Since only one can be chosen as "correct" I have chosen the one that brought, in addition to the explanation, a list of what is considered false or true by the
||
operator, but I strongly recommend reading the other answers.