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

  • Home
  • SEARCH
  • 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 6020777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:37:03+00:00 2026-05-23T03:37:03+00:00

Please explain the following way of writing a function in javascript functions : (function

  • 0

Please explain the following way of writing a function in javascript functions :

(function (){
 // some code 
})()

I understand the fact that because of the trailing braces ” () “, the function will execute immediately but but what does enclosing the function in the braces mean?

  • 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-05-23T03:37:03+00:00Added an answer on May 23, 2026 at 3:37 am

    Your syntax is incorrect. Edit: fixed.

    Look at a normal function definition that you want to call once and only once:

    function add(x,y) {
       var z = x + y; 
       // do something with z
    }
    
    add(2,3);
    

    You could reduce this code so that you execute it straight away, there is no need to name it add:

    (function(x ,y) {
       var z = x + y; 
       // do something with z
    })(2,3)
    

    Note how we have surrounded the function with a set of parenthesis to make it a callable expression. This pattern is often used to create a closure (capture the state of) certain variables, for example:

    jQuery.noConflict(); //Disables the use of $ in the global scope
    (function($) {
    
       //Safely use $ inside this function
       $('.link').click(function(e) {
          //etc
       });
    
    })(jQuery);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

the following code is a textmate javascript snippet, can anyone explain it please? cuz
This is an easy way to earn some points. Please explain the following: class
Could anyone please explain the following line of code, found on http://docs.openttd.org/ai__cargo_8cpp_source.html return (AICargo::TownEffect)::CargoSpec::Get(cargo_type)->town_effect;
Can someone please explain the following How was the first JDK release unit Tested?
Can anyone please explain to me why the following two queries yield different results?
Can someone please explain what the & does in the following: class TEST {
Please explain what this task is about? Create a generic linked list class that
Please explain the following paragraph. The next question is whether we can assign a
Can anybody please explain the following line about the designated initializers: The initializer list
Is there someone who can please explain why the following is not working??: the

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.