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 8613205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:48:58+00:00 2026-06-12T04:48:58+00:00

Possible Duplicate: How do JavaScript closures work? I was playing around with the Google

  • 0

Possible Duplicate:
How do JavaScript closures work?

I was playing around with the Google Closure Compiler, putting in random code to see what it would do.

It rewrote one of my functions to look something like this:

(function(msg) { console.log(msg); })("Hello World!");​​​​​​​

Where it appears that "Hello World" is the argument passed as msg to the anonymous function preceding it. I was looking at it for a moment, and had thought that I had seen something similar in jQuery plugins that look something like:

(function( $ ) {
  ...
})(jQuery); 

Which now makes more sense to me, in the scope of conflicts with $. But what is the primary reason or purpose for passing arguments into an anonymous function like this? Why wouldn’t you simply define the arguments as variables within the function? Is there any performance or flexibility advantage to writing functions like this?

  • 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-12T04:48:59+00:00Added an answer on June 12, 2026 at 4:48 am

    There is one significant difference connected also to scope. The following code:

    (function(msg) { console.log(msg); })("Hello World!");​​​​​​​
    

    is in some circumstances cleaner in terms of namespace pollution than this:

    var msg = "Hello World!";
    console.log(msg);
    

    because the second code leaves variable after it is no longer needed, but may interfere with other parts of the code.

    This is especially important when you execute the mentioned code outside any other function: in such case msg variable would be available everywhere on the page, as global variable.

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

Sidebar

Related Questions

Possible Duplicate: How do JavaScript closures work? Javascript closures and side effects in plain
Possible Duplicate: Internet Explorer, Closure Compiler and Trailing Commas I've tried compressing my javascript
Possible Duplicate: How do JavaScript closures work? I still battle with the concept of
Possible Duplicate: JavaScript scope and closure What is this for? (function(){ //The code to
Possible Duplicate: What is the 'new' keyword in JavaScript? creating objects from JS closure:
Possible Duplicate: JavaScript: Why the anonymous function wrapper? I would like to ask you
Possible Duplicate: JavaScript: Why the anonymous function wrapper? Sometimes I see a structure like
Possible Duplicate: Event handlers inside a Javascript loop - need a closure? I have
Possible Duplicate: javascript substring How can I do substr (php) in JS? Example code
Possible Duplicate: JavaScript closures and variable scope Assign click handlers in for loop I

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.