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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:45:13+00:00 2026-05-23T08:45:13+00:00

function f1(){ var n=999; function f2(){ alert(n); // 999 } } is the function

  • 0
    function f1(){
        var n=999;
        function f2(){
            alert(n); // 999
        }
    }

is the function f2() a closure? if not? why?

but in this post. How do JavaScript closures work?
why it says:

function foo(x) {
  var tmp = 3;
  function bar(y) {
    alert(x + y + (++tmp));
  }
  bar(10);
}
foo(2)

That is not a closure.A closure is when you return the inner function. The inner function will close-over the variables of foo before leaving. why?

but i don’t know what’s the difference between the example i made and another i cited, one is not a closure.but one is. i think the two example is the same.

  • 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-23T08:45:14+00:00Added an answer on May 23, 2026 at 8:45 am

    Yes, f2 is a closure, since it accesses a variable (n) from an outer scope. (OK, it’s not really a closure — see update below).

    n was declared inside f1, not f2; this makes it belong to f1‘s scope. So when you create the function f2 which references n, it is a closure by definition, since it uses someone else’s variable.

    UPDATE

    Alright, if I understand the answer you’ve linked to correctly, it says that f2 is not a closure because it is merely accessing a variable within its scope (just like an if statement, which gets its own scope within the braces*, can use variables from the outer scope without needing a closure).

    * Update: Turns out that only functions get their own scope in Javascript, not any old blocks. But my point still stands…

    However, f2 would become a closure if it left f1‘s scope (e.g. by being returned); in that case, it would still have access to f1‘s variable n, even though f1‘s original scope would no longer exist (it’s exited when control leaves the function). f2 would have “closed over” f1‘s variables, thus artificially extending the lifespan of f1‘s scope.

    Personally, I would still call f2 a closure, even if it never leaves f1. If a function can become a closure simply by being used outside of its declaring scope, and its behaviour inside that scope is no different whether it’s technically a closure or not, then I see no point in making a distinction. I would even go so far as to say that it’s an implementation detail whether f2 is a closure or not, if it never leaves f1‘s scope.

    On the other hand, ignoring that distinction would mean that any function that uses a global variable would be called a “closure”, since it accesses a variable from an outer scope. So the fact that it becomes a closure only when it leaves the scope(s) that the variables it is using are defined in is a worthwhile (albeit subtle) distinction.

    I guess the clearest answer I can give is that it’s not a closure yet.

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

Sidebar

Related Questions

View this code: function testprecision(){ var isNotNumber = parseFloat('1.3').toPrecision(6); alert(typeof isNotNumber); //=> string }
I would try something like this but it isn't allowed. function GetDynamicModulesProperties() { var
I have this piece of code: $(#faq).click(function () { var url = $.get(faq, {
I have this function in my head: <head> window.onload = function(){ var x =
I have the following HTML / JS: <html> <body> <script> function foo(){ var html
I have an external JavaScript that contains: function random_imglink(){ var myimages=new Array() myimages[1]="http://sevir.sitegoz.com/jchs/Banner1.png" myimages[2]="http://sevir.sitegoz.com/jchs/banner2.png"
$(document).ready(function() { var test_func=function(ev) { alert(ev.keyCode); }; .... $(document).keydown(test_func(ev)); }); I wanna do the
(function() { var myFunc = { init : function() { alert(I need); document.getElementById(myDiv).innerHTML =
Using this code v=new Date(); var bxx=document.getElementById('bxx'); function t(){ n=new Date(); ss={time}; s=ss-Math.round((n.getTime()-v.getTime())/1000.); m=0;h=0;
I have this script: function slideSwitch() { var ae = jQuery('#featured-right a.active'); if (

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.