Some people are convinced that using undefined
directly is bad because someone could do window.undefined = "whatever"
. This is because, in ES5 and before (we'll see what comes later), undefined
is not a reserved word and can be used as an identifier.
The local var
prevents such a bad global variable poisoning - it introduces a local variable called "undefined" (which will shadow any bad variable that may exist) who's value defaults to the real undefined value because there is no assignment.
If I was so concerned (which I am not), I would do x !== (void 0)
. Unlike undefined
, the void
operator has always been a reserved word and so it is immune to aforementioned poisoning.