Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8148755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:42:30+00:00 2026-06-06T14:42:30+00:00

While viewing jQuery’s uncompressed source code I stumbled upon something I don’t quite understand.

  • 0

While viewing jQuery’s uncompressed source code I stumbled upon something I don’t quite understand. When the create their anonymous function, they place undefined as a 2nd argument. What is this doing, and why are they using undefined? Is it necessary to put undefined as an argument in an anonymous function? Below is an example of what I am talking about.

(function( window, undefined)  {
  ...code here
})( window );
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:42:31+00:00Added an answer on June 6, 2026 at 2:42 pm

    What this is doing, is reassigning undefined to undefined inside that closure. Thats a failsafe. Because other code may accidentally do something like

    undefined = something;
    console.log(undefined); // will output 'something'
    

    And thats valid in javascript(if the JS engine being used has not implemented ECMAScript 5 specification, in ECMAScript 5 spec undefined is non non-writable, MDN DOC ),

    Quote from MDN New_in_JavaScript 1.8.5 (ECMA 5) Page

    Changes to global objects

    Global objects made read only

    The NaN, Infinity, and undefined global
    objects have been made read only, per the ECMAScript 5 specification.

    And from ES5 Annotated Spec in Guthub

    ES5 spec Section x15.1.1.3

    15.1.1.3 undefined

    The value of undefined is undefined (see 8.1).

    This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

    Even if global undefined is not writable You can have a local variable named undefined and can mess up your code(mainly comparisons with undefined). But that’s your responsibility. You can have codes like

    (function(){
        console.log('Second Case: ');
        var undefined = 'Something';
        console.log(undefined); // Will log `something`
        var a ; // a is undefined
        console.log(a  ===  undefined); // false, as undefined is changed
        // you might expect a === undefined will return true, but as 
        // `undefined` is changed it will return false.
        console.log(a); // undefined
    })();
    

    Demo: http://jsfiddle.net/joycse06/V4DKN/

    However if undefined is writeable then the above assignment may hamper many comparison made with undefined after that line of code as undefined is not undefined anymore. It has some value now.

    So as they are calling that anonymous function like

    ( window ) // one argument only
    

    And receiving

    ( window, undefined)  // only window is passed when calling the function
              // Second argument is not passed means it's undefined
              // so undefined is restored to undefined inside that function
              // and no global accidental assignments can hamper jQuery's 
              // code using 'undefined' now
    

    That means inside that closure undefined is restored to undefined, as it has not been passed any value thus securing use of undefined inside that anonymous function.

    A very good detailed article on this http://javascriptweblog.wordpress.com/2010/08/16/understanding-undefined-and-preventing-referenceerrors/

    I am quoting some lines from the above article link to make things clear

    What is undefined?

    In JavaScript there is Undefined (type), undefined (value) and undefined (variable).

    Undefined (type) is a built-in JavaScript type.

    undefined (value) is a primitive and is the sole value of the Undefined type.

    Any property that has not been assigned a value, assumes the undefined value. (ECMA 4.3.9
    and 4.3.10).

    A function without a return statement, or a function with an empty return statement returns undefined. The value of an unsupplied function argument is undefined.

    var a;
    typeof a; //"undefined"
    
    window.b;
    
    typeof window.b; //"undefined"
    
    
    
    var c = (function() {})();
    
    typeof c; //"undefined"
    
    
    
    var d = (function(e) {return e})();
    
    typeof d; //"undefined"
    

    undefined (variable) is a global property whose initial value is undefined (value), Since its a global property we can also access it as a variable. For consistency I’m always going to call it a variable in this article.

    typeof undefined; //"undefined"
    var f = 2;
    f = undefined; //re-assigning to undefined (variable)
    typeof f; //"undefined"
    

    As of ECMA 3, its value can be reassigned :

    undefined = "washing machine"; //assign a string to undefined (variable)
    typeof undefined //"string"
    f = undefined;
    typeof f; //"string"
    f; //"washing machine"
    

    Needless to say, re-assigning values to the undefined variable is very bad practice, and in fact its not allowed by ECMA 5.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can only see inline CSS and JS while viewing the source code of
While viewing the source of a web page, I came across this CSS, applied
I noticed this interesting use of the this keyword while viewing the disassembled code
basically, i was wiresharking packets on my PS3 while viewing Motorstorm Leaderboards. The leaderboards
While I understand the Math.round/ceil/floor functions in javascript, I have been unable to come
I'm using WAMP. I want to measure the bytes sent and received while viewing
This was my first go at writing some jQuery code completely from scratch, so
While viewing Evans' project on sample DDD project, I notice that in the Cargo
When viewing the site I'm working on in IE7 my Reservations area (jquery ui
I am having a html file containing a div tag. While viewing this html

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.