Create array and push into it in one line

You’ve got your assignment backwards*. It should be:

(window.foo = window.foo || []).push('bar');

The || operator in JavaScript does not return a boolean value. If the left hand side is truthy, it returns the left hand side, otherwise it returns the right hand side.

a = a || [];

is equivalent to

a = a ? a : [];

So an alternative way of writing the above is:

(window.foo = window.foo ? window.foo : []).push('bar');

* see comments for details

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)