Skip to content

Instantly share code, notes, and snippets.

@peterkc
Created March 4, 2014 16:54
Show Gist options
  • Save peterkc/9350438 to your computer and use it in GitHub Desktop.
Save peterkc/9350438 to your computer and use it in GitHub Desktop.
What is the purpose of passing-in undefined?

There are two reasons for that:

  1. Since undefined is a property of the global object, without write-protection, it can be overridden which may result in strange and unexpected behavior when trying this: ( var == undefined).

By having an function argument undefined (the name actually does not matter) which you don't pass a parameter to, you make sure you have a variable which really is undefined so you can test "undefinedness" against this variable.

Btw. the safest method to test for undefined is: typeof ( var ) === 'undefined'

EDIT: With adoption of EcmaScript 5, undefined, NaN and Infinity are now readonly in all modern browsers - of course with the exception of Internet Explorer up to Version 9 :(. So this is not necessary anymore, however:

  1. Since undefined is now a variable name in this function's scope rather than a global object property, minifiers can reduce it to a single letter thus achieving a better compression rate on the minified file.

http://stackoverflow.com/questions/9602901/what-is-the-purpose-of-passing-in-undefined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment