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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:17:41+00:00 2026-06-04T11:17:41+00:00

I am looking at this function function foo(x) { var tmp = 3; return

  • 0

I am looking at this function

function foo(x) {
  var tmp = 3;
  return function (y) {
    alert(x + y + (++tmp));
  }
}
var bar = foo(2); // bar is now a closure.
bar(10);

when I run it, the variables get the following values

x = 2,
y = 10
tmp = 3.

Now I see that in foo(2) x is passed as 2. So its understandable that x is getting the value of 2. But then bar(10) is assigning a value of 0 to y. Hows that? I am confused on how does the receiving function know that 10 is the value for y assigned by bar(10)

  • 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-04T11:17:43+00:00Added an answer on June 4, 2026 at 11:17 am

    foo(2) returns an anonymous function which accepts one parameter (y). As you’re setting bar to be the return value of foo(2), bar becomes a reference to that anonymous function.

    So, when you call bar(10) you’re calling the anonymous function foo returns, and so 10 is being set to the parameter y.

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

Sidebar

Related Questions

Given: function foo(){ var bar = quux; console.log(/*mystery code here*/); } I'm looking for
Consider the following code: function Dog() { this.foo = function() {}; this.walk = function()
$bar = 'BAR'; apc_store('foo', $bar); var_dump(apc_fetch('foo')); Within one request this work. Now If i
Lets imagine function like this: function foo(x) { x += '+'; return x; }
I am looking to scroll the page to a particular location.Something like this: function
Today, looking at the man page for open() , I've noticed this function is
I'm looking to add double buffering to a drawing function like this. dc =
I am looking for a function to create a so called Beeswarm boxplot. This
Recently I've been looking at opening a tab using this script: $('.tofour').click(function() { //
Looking in this StackOverflow question it uses the following to send emails: System.Net.Mail.SmtpClient Is

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.