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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:08:15+00:00 2026-06-06T08:08:15+00:00

Possible Duplicate: Difference between (function(){})(); and function(){}(); I am trying to understand a few

  • 0

Possible Duplicate:
Difference between (function(){})(); and function(){}();

I am trying to understand a few of the features of JavaScript a little better. In The Definitive JavaScript it says that self-executing functions should have brackets round them like so:

var obj = (function() {
    var value = 0;

    return {
        increment: function(inc) {
            value += typeof inc === "number" ? inc : 1;
        },
        getValue: function() {
            return value;
        }
    }
})();

but in JavaScript - The Good Parts where this example is taken from, it has the above self-executing function without the brackets round, like so:

var obj = function() {
    var value = 0;

    return {
        increment: function(inc) {
            value += typeof inc === "number" ? inc : 1;
        },
        getValue: function() {
            return value;
        }
    }
}();

Both of these examples work for me, but I wanted to ask if there were any differences in functionality that I should be aware of. I hope this isn’t too trivial. I just wanted to be sure.

Thanks a lot.

Edit:

As Rob W has pointed out, there is another thread on the subject. This is an excellent blog regarding this issue that was linked to from the other post.

  • 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-06T08:08:17+00:00Added an answer on June 6, 2026 at 8:08 am

    There isn’t any difference in this case, but only because it’s prefixed with:

    var obj = ...
    

    Without that, only the first version is correct, because you need the additional parentheses to allow the interpreter to correctly parse the function as a function expression and not as a function declaration.

    You would of course only omit var obj if you only want the function to run (i.e. you want its side effects) but it either returns no value, or you don’t care what its return value is.

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

Sidebar

Related Questions

Possible Duplicate: What is the difference between a function expression vs declaration in JavaScript?
Possible Duplicate: What is the difference between a function expression vs declaration in Javascript?
Possible Duplicate: Difference between try-catch syntax for function Few days ago I was reading
Possible Duplicate: What is the difference between a method and a function I'm trying
Possible Duplicate: What is the difference between a function expression vs declaration in Javascript?
Possible Duplicate: What is the difference between a ‘function’ and a ‘procedure’? I searched
Possible Duplicate: JavaScript: Class.method vs. Class.prototype.method What's the difference between creating a prototype like
Possible Duplicate: JavaScript: Class.method vs. Class.prototype.method I am trying to understand object in JavaScript.
Possible Duplicate: Difference between try-catch syntax for function What is the difference in the
Possible Duplicates: What is the difference between a function expression vs declaration in JavaScript?

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.