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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:42:59+00:00 2026-06-12T20:42:59+00:00

Recently I was reading someone else’s code, and came across this: // Semicolon (;)

  • 0

Recently I was reading someone else’s code, and came across this:

// Semicolon (;) to ensure closing of earlier scripting
// Encapsulation
// $ is assigned to jQuery
;(function($) {

     // DOM Ready
    $(function() {
        ...
  });

})(jQuery);

I understand the point of the leading ;, And I understand that $(function() { is the same as document ready, but what is the point of adding function($)?

I understand it’s a closure, but since this is always being called at the global scope, it seems like you don’t need to bother with it. The $(function() { will use the same global object either way, no?

Is it to safeguard against something, or is it a best practice for another reason?

  • 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-12T20:43:00+00:00Added an answer on June 12, 2026 at 8:43 pm

    It’s a common structure for a jQuery plugin. It safeguards against the $ identifier having been overwritten and used for something else. Inside the anonymous function, $ is always going to refer to jQuery.

    Example:

    $ = "oh no";
    $(function() { //Big problem!
        //DOM ready
    });
    

    By introducing a new scope, you can ensure that $ refers to what you expect it to:

    $ = "oh no";
    (function($) { //New scope, $ is redeclared and jQuery is assigned to it
    
        $(function() { //No problem!
            //DOM ready
        }); 
    
    }(jQuery));
    

    The main reasoning behind this is that numerous other JavaScript libraries use $ as an identifier (e.g. PrototypeJS). If you wanted to use both Prototype and jQuery, you need to let Prototype have its $ identifier, but you probably don’t want to write out jQuery every time you want to call a jQuery method. By introducing a new scope you allow jQuery to have its $ back in that execution context.

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

Sidebar

Related Questions

I recently came across this expression - but reading up on Wikipedia did not
So recently am reading a book called Adaptive Webdesign and I came across something
I was recently reading a book as prep for an interview and came across
I recently inherited some code that someone else had written. I discovered that everywhere
I have recently reading someone code. In his code I see a weird html
I was recently reading this discussion at SO where somebody commented that not all
I was reading a forum recently, and saw this comment: So, you see you've
Recently I'm reading the Glibc(2.16) source code on its newest release. While on the
I was recently reading a question here (some time this week), which I can't
This is an issue that recently came up for me while writing a new

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.