Good day, was reading a JavaScript script library and come across this
var g = g || {};
What does this means?
Good day, was reading a JavaScript script library and come across this
var g = g || {};
What does this means?
This is a way to ensure g is actually initialized as an object. It is the same as:
if(!g) g = {};
The || is an OR. The second operand will be returned only if the first one evaluates to false.
It means, that if g is 0/null/undefined, the g will be defined as an empty object.
The common way:
function foo(g) {
    // If the initial g does not defined, null or 0
    if(!g) {
      // Define g as an empty object
      g = {}
    }
}
JavaScript returns the first argument if true, otherwise the second one. This is equivalent to an OR.
So in your example, if g is not set, it is set to an empty object.
Following description is taken from this page.
expr1 || expr2 Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand can be converted to true; if both can be converted to false, returns false.
In your case, if g has a falsy value (false/null/undefined/0/NaN/''/(document.all)[1]), then g will be set with {}