Using the || operator like that is a pretty common paradigm in JavaScript. I'll attempt to explain...

As you're probably aware, JavaScript has implicit type conversion. When a boolean is required, some values correspond to true and some to false. These are commonly referred to as truthy and falsey values respectively. false, 0, "", NaN, null and undefined are all falsey.

The || operator will return the left-hand argument if the left-hand argument is truthy. Otherwise it will return the right-hand argument. Something a bit like this:

Code:

function OR(a, b) {
if (a) {
return a;
}
return b;
}

It does not convert the return value to a boolean as part of this process. If you draw up a truth table, or just stare at it long enough, you should eventually be able to convince yourself that this way of evaluating || actually does correspond to an OR operator. If either input value is truthy then the output will also be truthy. Only if both values are falsey will the output be falsey.

So, for example, if you write this:

Code:

var c = 0 || null;

Both arguments are falsey. c will be null, the right-hand argument, which is also falsey.

As another example, consider:

Code:

var d = null || 'hello';

Here the left-hand argument is falsey but the right-hand argument is truthy. d will end up being 'hello', which is truthy.

A consequence of this implementation of || is that it can be used to provide default values. This is what is happening in the example with the proxy. Again, if this isn't obvious then stare at it, break out a pen and paper and convince yourself that this is true. Consider what a default value function might look like, you'll probably end up with something similar to the OR function I wrote earlier.

The && operator is similar. You could think of it as follows:

Code:

function AND(a, b) {
if (a) {
return b;
}
return a;
}

The && operator can also be used to do trickery, just like the || operator, but it is much less common.