Operator || JavaScript is similar to the "or" operator in other languages ββlike C, but it is distinctly different. It really means:
- Rate the subexpression on the left.
- If this value when enforced to a boolean value is
true , then this value of the subexpression (before coercion to a boolean) is the value of the expression || - Else evaluates the right subexpression and displays its value as the value of the expression
|| .
Thus, he idiomatically used for initialization what can already be initialized:
var something = window.something || defaultValue;
just means: "check to see if" something "is a property of a window object with a true value, and if not, set its default value."
source share