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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:28:04+00:00 2026-05-28T16:28:04+00:00

Possible Duplicate: What is the (function() { } )() construct in JavaScript? I came

  • 0

Possible Duplicate:
What is the (function() { } )() construct in JavaScript?

I came across this bit of JavaScript code, but I have no idea what to make out of it. Why do I get “1” when I run this code? What is this strange little appendix of (1) and why is the function wrapped in parentheses?

(function(x){
    delete x;
    return x;
})(1);
  • 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-28T16:28:05+00:00Added an answer on May 28, 2026 at 4:28 pm

    There are a few things going on here. First is the immediately invoked function expression (IIFE) pattern:

    (function() {
      // Some code
    })();
    

    This provides a way to execute some JavaScript code in its own scope. It’s usually used so that any variables created within the function won’t affect the global scope. You could use this instead:

    function foo() {
      // Some code
    }
    foo();
    

    But this requires giving a name to the function, which is not always necessary. Using a named function also means at some future point the function could be called again which might not be desirable. By using an anonymous function in this manner you ensure it’s only executed once.

    This syntax is invalid:

    function() {
      // Some code
    }();
    

    Because you have to wrap the function in parentheses in order to make it parse as an expression. More information is here: http://benalman.com/news/2010/11/immediately-invoked-function-expression/

    So to recap quickly on the IIFE pattern:

    (function() {
      // Some code
    })();
    

    Allows ‘some code’ to be executed immediately, as if it was just written inline, but also within its own scope so as not to affect the global namespace (and thus potentially interfere with or be interfered with by, other scripts).

    You can pass arguments to your function just as you would a normal function, for example,

    (function(x) {
      // Some code
    })(1);
    

    So we’re passing the value ‘1’ as the first argument to the function, which receives it as a locally scoped variable, named x.

    Secondly, you have the guts of the function code itself:

    delete x;
    return x;
    

    The delete operator will remove properties from objects. It doesn’t delete variables. So;

    var foo = {'bar':4, 'baz':5};
    delete foo.bar;
    console.log(foo);
    

    Results in this being logged:

    {'baz':5}
    

    Whereas,

    var foo = 4;
    delete foo;
    console.log(foo);
    

    will log the value 4, because foo is a variable not a property and so it can’t be deleted.

    Many people assume that delete can delete variables, because of the way autoglobals work. If you assign to a variable without declaring it first, it will not actually become a variable, but a property on the global object:

    bar = 4; // Note the lack of 'var'. Bad practice! Don't ever do this!
    delete bar;
    console.log(bar); // Error - bar is not defined.
    

    This time the delete works, because you’re not deleting a variable, but a property on the global object. In effect, the previous snippet is equivalent to this:

    window.bar = 4;
    delete window.bar;
    console.log(window.bar);
    

    And now you can see how it’s analogous to the foo object example and not the foo variable example.

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

Sidebar

Related Questions

Possible Duplicate: JavaScript Function Definition in ASP User Control Hi, I have a generic
Possible Duplicate: JavaScript function aliasing doesn't seem to work Why doesn't this work? function
Possible Duplicate: Should a function have only one return statement? This is what I
Possible Duplicate: javascript function vs. ( function() { … } ()); Sorry if this
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the
Possible Duplicate: JavaScript: Why the anonymous function wrapper? I would like to ask you
Possible Duplicate: How does foreach work when looping through function results? If I have
Possible Duplicate: User control javascript I defined a JavaScript function inside a user control.
Possible Duplicate: How can I pre-set arguments in JavaScript function call? (Partial Function Application)
Possible Duplicate: Call php function from javascript I understand that php is server side

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.