Which method of checking if a variable has been initialized is better/correct? (Assuming the variable could hold anything (string, int, object, function, etc.))
In the particular situation outlined in the question,
typeof window.console === "undefined"
is identical to
window.console === undefined
I prefer the latter since it's shorter.
Please note that we look up for console
only in global scope (which is a window
object in all browsers). In this particular situation it's desirable. We don't want console
defined elsewhere.
@BrianKelley in his great answer explains technical details. I've only added lacking conclusion and digested it into something easier to read.