These are VERY different.
First, without checking for typeof ... === 'undefined'
you will essentially rewrite any 'falsy' values. But that's trivial, there are more subtle nuances.
window.x = typeof x != "undefined" ? x : {}
This will assign value of some x
(not necessary global, it may be a local x
or x
local to some outer function) to global x
(window.x
). When that 'local' x will go out of scope, its value at the moment of assignment will still be stored in window.x
variable.
window.x = window.x || {}
This works with window.x
only; even if we forget at the moment about falsy values, it still does not work the same as the first one (it doesn't even check for 'local' x
existence).
x = this.x || {}
And this one may go completely haywire when this
is volatile (event handlers, timeout functions, to name a few), and will not be allowed within outer functions' body in 'use strict' mode.