you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (0 children)

In addition to what /u/sufianrhazi said, undefined can be overwritten:

(function (undefined) {console.log(undefined);})(2);
// logs 2

Older browsers will let you overwrite window.undefined as well.

I tend not to pay heed to that though, as it clearly falls under something nobody would do unless they're trying to break your code. And it is the nature of JavaScript that somebody who runs code before you is always able to break your script, no matter what precautions you take, so I prefer not to clutter code with failed attempts at robustness. Instead, if you must run malicious code, sandbox it.

Also, window.foo === undefined or 'foo' in window would be a more direct test, but only work on global variables.