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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:21:35+00:00 2026-05-18T08:21:35+00:00

var foo = (function(){ var x = 0; return function(){return x++;}; })() Why the

  • 0
var foo = (function(){
  var x = 0;
  return function(){return x++;};
})()

Why the var x = 0 expression only runs once is my biggest misunderstanding about 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-05-18T08:21:35+00:00Added an answer on May 18, 2026 at 8:21 am

    Your code:

    var foo = (function(){
      var x = 0;
      return function(){return x++;};
    })()
    

    is equivalent to this code:

    function f(){
      var x = 0;
      return function(){return x++;};
    }
    var foo = f();
    

    It’s easy to see, when you break it up like this, that the function f() is only called once. It defines x, and then returns a new function that is defined inside the local scope of f. This new function is often called an “anonymous function” (meaning that it has no name) or a “closure”. In truth, all functions in javascript are “closures” — whether or not they are named. The term “closure” simply means that the function retains access to the variables that were defined in the parent function’s scope — even after the parent function has exited.

    So now, foo contains the new function (the closure) that was returned from f. You can call foo() as many times as you like — and each time you do, x will be returned and post-incremented. Since x exists in the closure’s parent scope, its value will persist across multiple calls to the closure.

    What’s more… no other code now has access to x once f() has exited — this basically means that x is now the “private data” of the closure. Pretty neat huh?

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

Sidebar

Related Questions

var foo = { x: 1, y: (function () { return ++this.x; })() };
I'm unclear about return value of the following dummy code: function foo() var ret
var foo = { bar: function() { return this.baz; }, baz: 1 }; (function(){
In this code: <?php class Foo { var $value; function foo($value) { $this->setValue($value); }
This is what I'm doing right now. var foo = function() { var x
I have this Car function: var Car = function(vendor, model, year) { return {
My model and collection looks like this. var MyModel= Backbone.Model.Extend({ foo: function(){ alert(is not
With following code: var App = (function(param) { return { foo : function() {
var foo = function(){ return 1; }; if (true) { function foo(){ return 2;
so I have: var something = function () { return 6} var foo =

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.