0

In JavaScript undefined is not a keyword, it is a variable. Sort of special variable. This is known to cause trouble because undefined could be reassigned. I do not really know why would anyone want to do that, but this is possible, and maybe you can experience this by using one of those genius frameworks out there, see the code:

function f() {
    var z, undefined = 42
    console.log(z, undefined, z == undefined)
} f()

Outputs:

undefined 42 false

Now how does one protect himself from such a confusion? Should one just cross his fingers that undefined is undefined?

exebook
  • 27,243
  • 27
  • 105
  • 196
  • 6
    What normal javascript developer would ever use `undefined` as a variable name? If someone does, then he deserves to be bitten and read/learn more about the language he is using. – Darin Dimitrov Jan 01 '14 at 14:27
  • 2
    This kind of situation was covered in this (humorous) [talk](https://www.destroyallsoftware.com/talks/wat). More seriously, since JavaScript has only fairly recently become standardized, it has lots of little quirks. – Elad Stern Jan 01 '14 at 14:28
  • @Givi, what do you mean? – exebook Jan 01 '14 at 14:35
  • @EladStern: JavaScript was standardized **at least** 16-17 years ago, in the form of the ECMAScript First Edition specification in 1997. The third edition spec was released 14-15 years ago in 1999. Granted it's taken some implementations a long time to finally get it right, and the fifth edition (there was no fourth) is only three years old or so. – T.J. Crowder Jan 01 '14 at 14:36
  • Look at [What advantages does using (function(window, document, undefined) { … })(window, document) confer?](http://stackoverflow.com/questions/5020479/what-advantages-does-using-functionwindow-document-undefined-windo) or [How does this JavaScript/JQuery Syntax work: (function( window, undefined ) { })(window)?](http://stackoverflow.com/questions/2716069/how-does-this-javascript-jquery-syntax-work-function-window-undefined) – Givi Jan 01 '14 at 14:37
  • 4
    *"JavaScript: how to protect from “undefined” is defined confusion?"* I find taking anyone who redefines `undefined` out to the back and administering a really good beating generally does the trick. – T.J. Crowder Jan 01 '14 at 14:40

3 Answers3

4

Just use void 0, it's simply bulletproof. And it's a keyword too.

Esailija
  • 130,716
  • 22
  • 250
  • 308
3

You can pass undefined as a function parameter, this will ensure that undefined is undefined in the scope of the function.

Many JavaScript libraries use this technique, for example look at jQuery source code

//jQuery
(function( window, undefined ) {
...
})( window ); 

Because the function expects two formal parameters, but we only give it one, the other gets the (true) value undefined, and then we can rely on that within the function.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
aljordan82
  • 1,660
  • 1
  • 16
  • 22
0

one possibility is to check the type of the variable instead of checking equality to undefied:

if (typeof(VARIABLE) != "undefined") 

or read on here: How to check for “undefined” in JavaScript?.

Community
  • 1
  • 1
sailingthoms
  • 840
  • 10
  • 22
  • 2
    @exebook `typeof x EQ CONSTANT_STRING` is a pattern specifically optimized for - there is no string comparison unless you deviate from the pattern. That means that it's much more efficient to write `typeof x === "string" || typeof x === "number"` because those don't deviate from the pattern whereas saving it to a variable would... – Esailija Jan 01 '14 at 14:33
  • @exebook: Worry about it ***if and when*** it's a problem. – T.J. Crowder Jan 01 '14 at 14:33
  • 2
    @ sailingthoms: `typeof` is not a function, there's no reason to wrap its operand in parentheses. It makes no more sense than `a = (1) + (5);`. – T.J. Crowder Jan 01 '14 at 14:34
  • yes, but I think it helps in getting the point, even if it's not neccesary to use parentheses – sailingthoms Jan 01 '14 at 14:34