In general, you should only use βshortcutsβ like this if it makes the code more readable for a regular JavaScript programmer than an alternative.
If you are thinking of something that is more readable and less surprising, think that
var foo; if(bar) { foo=[]; }
and
var foo = bar && [];
do not match. For example, if bar is NaN , then foo will be NaN in a later case, which later may be a little head scratcher.
Since there are tools to optimize / minimize JavaScript, you should focus on the readability of your code, which is not always the same as brevity.
Suppose you have several such repeated initializations in a string, they all depend on different variables (so they cannot be wrapped in one conditional), but follow the same logical formula. In this case, when the reader mentally analyzed the meaning of the formula, they could quickly look through all the instances and see the differences between them. In this case, instead of relying on a convention that most JavaScript programmers are familiar with (for example, var foo = some_opt || {} ), you create a localized convention that the reader will need to learn only for this file. Even in this case, I would advise you to carefully consider it, maybe not worth it.
source share