tl;dr; No. You’re welcome.
It was bad
If you don’t know, only recently (IE9?) did undefined become immutable. Previously, you could put undefined = ‘weee’; in your code and suddenly undefined stopped working everywhere.
What about now?
But today, I’ve decided to give myself a refresher and look up… what exactly is the state of undefined in js?
So… now undefined is now immutable. I can use it, right? Apparently not. Summary: it can still shit the bed in a function (confirmed in node 0.10.28).
FML. Stuck with typeof blah === ‘undefined’ then? Not exactly.
A kinda hacky workaround
Enter void 0.
Another thing that I’ve always aVOIDed is void (wordplay is the lowest form of humor). I apparently absorbed that through the ethos from Crockford, as it was one of his bad parts of JS.
But turns out, void 0 *always* returns undefined (the real undefined), and can be used to strict compare; like var b; b === void 0; // true.
Yay! I’ll just use void 0! Well… almost. GOTO FML.
You’ll get an exception if you try to compare against a variable not in the local scope. That is… blah === void 0 = exception. var blah; blah === void 0 = true.
However! We have the in operator for non-local vars — which I (terribly) never used, for no reason in particular.
It’s better not to play. Stuff should be defined unless you know better.
If you absolutely need to check though:
- Local: localBlah === void 0
- Global: ‘whatever’ in window (global) — be sure to see these caveats here especially with regard to deleted/undefined properties.
Bye bye typeof/undefined.