I like that syntax. In my opinion, it is very elegant.
The explanation is quite simple. You have a conditional expression here using the || (or). This will assign the value to the variable according to the result of the conditional expression.
In this case, the condition a || { b : 1 } a || { b : 1 } , therefore, if the variable a already defined (that is, it is not equal to a false value), then the variable a will remain unchanged. However, if a is not yet defined, then it will be assigned the object literal value of { b : 1 } .
This syntax method is commonly used to determine default values.
For instance:
function say_hello( name ){ var the_name = name || 'Dude'; alert( "Hello, " + the_name ); } say_hello( "Lix" );
If the name argument was not passed to the function, the default name is Dude .
source share