Why do variable in an inner function return nan when there is the same variable name at the inner function declared after log The Next CEO of Stack OverflowWhat is the naming convention in Python for variable and function names?How to execute a JavaScript function when I have its name as a stringWhat is a practical use for a closure in JavaScript?Javascript by reference vs. by valueWhy aren't ◎ܫ◎ and ☺ valid JavaScript variable names?What is the explanation for these bizarre JavaScript behaviours mentioned in the 'Wat' talk for CodeMash 2012?Is the recommendation to include CSS before JavaScript invalid?variable not writable in inner functionjavascript variable returning NaNFunction returns NaN when it shouldn't

What's the best way to handle refactoring a big file?

Why does the UK parliament need a vote on the political declaration?

Return the Closest Prime Number

If Nick Fury and Coulson already knew about aliens (Kree and Skrull) why did they wait until Thor's appearance to start making weapons?

Multiple labels for a single equation

I believe this to be a fraud - hired, then asked to cash check and send cash as Bitcoin

Are there any limitations on attacking while grappling?

What exact does MIB represent in SNMP? How is it different from OID?

Which tube will fit a -(700 x 25c) wheel?

How did the Bene Gesserit know how to make a Kwisatz Haderach?

How to safely derail a train during transit?

In excess I'm lethal

What can we do to stop prior company from asking us questions?

Which kind of appliances can one connect to electric sockets located in an airplane's toilet?

Why do remote companies require working in the US?

What does convergence in distribution "in the Gromov–Hausdorff" sense mean?

Is it my responsibility to learn a new technology in my own time my employer wants to implement?

How do I reset passwords on multiple websites easily?

Is there a way to save my career from absolute disaster?

Why do variable in an inner function return nan when there is the same variable name at the inner function declared after log

sp_blitzCache results Memory grants

Sending manuscript to multiple publishers

Why do professional authors make "consistency" mistakes? And how to avoid them?

What was the first Unix version to run on a microcomputer?



Why do variable in an inner function return nan when there is the same variable name at the inner function declared after log



The Next CEO of Stack OverflowWhat is the naming convention in Python for variable and function names?How to execute a JavaScript function when I have its name as a stringWhat is a practical use for a closure in JavaScript?Javascript by reference vs. by valueWhy aren't ◎ܫ◎ and ☺ valid JavaScript variable names?What is the explanation for these bizarre JavaScript behaviours mentioned in the 'Wat' talk for CodeMash 2012?Is the recommendation to include CSS before JavaScript invalid?variable not writable in inner functionjavascript variable returning NaNFunction returns NaN when it shouldn't










6















What's happening here? I get a different result if I declare a variable after console.log in the inner function



I understand that var has a functional scope and inner function can access the variable from their parent






function outer() 
var a = 2;

function inner()
a++;
console.log(a) //log NaN
var a = 8

inner()

outer()








function outer() 
var a = 2;

function inner()
a++;
console.log(a) //log 3
var b = 8

inner()

outer()





The log returns NaN in the first example and log 3 in the second example










share|improve this question




























    6















    What's happening here? I get a different result if I declare a variable after console.log in the inner function



    I understand that var has a functional scope and inner function can access the variable from their parent






    function outer() 
    var a = 2;

    function inner()
    a++;
    console.log(a) //log NaN
    var a = 8

    inner()

    outer()








    function outer() 
    var a = 2;

    function inner()
    a++;
    console.log(a) //log 3
    var b = 8

    inner()

    outer()





    The log returns NaN in the first example and log 3 in the second example










    share|improve this question


























      6












      6








      6








      What's happening here? I get a different result if I declare a variable after console.log in the inner function



      I understand that var has a functional scope and inner function can access the variable from their parent






      function outer() 
      var a = 2;

      function inner()
      a++;
      console.log(a) //log NaN
      var a = 8

      inner()

      outer()








      function outer() 
      var a = 2;

      function inner()
      a++;
      console.log(a) //log 3
      var b = 8

      inner()

      outer()





      The log returns NaN in the first example and log 3 in the second example










      share|improve this question
















      What's happening here? I get a different result if I declare a variable after console.log in the inner function



      I understand that var has a functional scope and inner function can access the variable from their parent






      function outer() 
      var a = 2;

      function inner()
      a++;
      console.log(a) //log NaN
      var a = 8

      inner()

      outer()








      function outer() 
      var a = 2;

      function inner()
      a++;
      console.log(a) //log 3
      var b = 8

      inner()

      outer()





      The log returns NaN in the first example and log 3 in the second example






      function outer() 
      var a = 2;

      function inner()
      a++;
      console.log(a) //log NaN
      var a = 8

      inner()

      outer()





      function outer() 
      var a = 2;

      function inner()
      a++;
      console.log(a) //log NaN
      var a = 8

      inner()

      outer()





      function outer() 
      var a = 2;

      function inner()
      a++;
      console.log(a) //log 3
      var b = 8

      inner()

      outer()





      function outer() 
      var a = 2;

      function inner()
      a++;
      console.log(a) //log 3
      var b = 8

      inner()

      outer()






      javascript function






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 18 mins ago









      Nick Parsons

      10.3k2926




      10.3k2926










      asked 27 mins ago









      ClaudeClaude

      426




      426






















          3 Answers
          3






          active

          oldest

          votes


















          8














          This is due to hoisting



          The declaration of a in the inner function is hoisted to the top of the function, overriding the outer function's a, so a is undefined



          undefined++ returns NaN, hence your result.



          Your code is equivalent to:



          function outer() 
          var a=2;

          function inner()
          var a;
          a++;
          console.log(a); //log NaN
          a = 8;


          inner();


          outer();


          Rewriting your code in this way makes it easy to see what's going on.






          share|improve this answer
































            1














            Because var is hoisted through the function, you're essentially running undefined++ which is NaN. If you remove var a = 8 in inner, the code works as expected:






            function outer() 
            var a = 2;

            function inner()
            a++;
            console.log(a);

            inner();

            outer();








            share|improve this answer






























              -1














              var a=0;
              function outer()
              a=2;
              function inner()
              a=a+1;
              console.log(a)
              a = 8

              inner()

              outer()





              share|improve this answer


















              • 3





                How does this piece of code explains the issue? Can you provide an explanation of the code you have posted?

                – Shidersz
                12 mins ago











              • They can’t access the inner function value so we have to defined globally. After globally you can use A value anywhere in the code

                – Darshit Shah
                10 mins ago











              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55428371%2fwhy-do-variable-in-an-inner-function-return-nan-when-there-is-the-same-variable%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              8














              This is due to hoisting



              The declaration of a in the inner function is hoisted to the top of the function, overriding the outer function's a, so a is undefined



              undefined++ returns NaN, hence your result.



              Your code is equivalent to:



              function outer() 
              var a=2;

              function inner()
              var a;
              a++;
              console.log(a); //log NaN
              a = 8;


              inner();


              outer();


              Rewriting your code in this way makes it easy to see what's going on.






              share|improve this answer





























                8














                This is due to hoisting



                The declaration of a in the inner function is hoisted to the top of the function, overriding the outer function's a, so a is undefined



                undefined++ returns NaN, hence your result.



                Your code is equivalent to:



                function outer() 
                var a=2;

                function inner()
                var a;
                a++;
                console.log(a); //log NaN
                a = 8;


                inner();


                outer();


                Rewriting your code in this way makes it easy to see what's going on.






                share|improve this answer



























                  8












                  8








                  8







                  This is due to hoisting



                  The declaration of a in the inner function is hoisted to the top of the function, overriding the outer function's a, so a is undefined



                  undefined++ returns NaN, hence your result.



                  Your code is equivalent to:



                  function outer() 
                  var a=2;

                  function inner()
                  var a;
                  a++;
                  console.log(a); //log NaN
                  a = 8;


                  inner();


                  outer();


                  Rewriting your code in this way makes it easy to see what's going on.






                  share|improve this answer















                  This is due to hoisting



                  The declaration of a in the inner function is hoisted to the top of the function, overriding the outer function's a, so a is undefined



                  undefined++ returns NaN, hence your result.



                  Your code is equivalent to:



                  function outer() 
                  var a=2;

                  function inner()
                  var a;
                  a++;
                  console.log(a); //log NaN
                  a = 8;


                  inner();


                  outer();


                  Rewriting your code in this way makes it easy to see what's going on.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 9 mins ago









                  Shidersz

                  9,3112933




                  9,3112933










                  answered 21 mins ago









                  jrojro

                  552112




                  552112























                      1














                      Because var is hoisted through the function, you're essentially running undefined++ which is NaN. If you remove var a = 8 in inner, the code works as expected:






                      function outer() 
                      var a = 2;

                      function inner()
                      a++;
                      console.log(a);

                      inner();

                      outer();








                      share|improve this answer



























                        1














                        Because var is hoisted through the function, you're essentially running undefined++ which is NaN. If you remove var a = 8 in inner, the code works as expected:






                        function outer() 
                        var a = 2;

                        function inner()
                        a++;
                        console.log(a);

                        inner();

                        outer();








                        share|improve this answer

























                          1












                          1








                          1







                          Because var is hoisted through the function, you're essentially running undefined++ which is NaN. If you remove var a = 8 in inner, the code works as expected:






                          function outer() 
                          var a = 2;

                          function inner()
                          a++;
                          console.log(a);

                          inner();

                          outer();








                          share|improve this answer













                          Because var is hoisted through the function, you're essentially running undefined++ which is NaN. If you remove var a = 8 in inner, the code works as expected:






                          function outer() 
                          var a = 2;

                          function inner()
                          a++;
                          console.log(a);

                          inner();

                          outer();








                          function outer() 
                          var a = 2;

                          function inner()
                          a++;
                          console.log(a);

                          inner();

                          outer();





                          function outer() 
                          var a = 2;

                          function inner()
                          a++;
                          console.log(a);

                          inner();

                          outer();






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 19 mins ago









                          Jack BashfordJack Bashford

                          13.8k31848




                          13.8k31848





















                              -1














                              var a=0;
                              function outer()
                              a=2;
                              function inner()
                              a=a+1;
                              console.log(a)
                              a = 8

                              inner()

                              outer()





                              share|improve this answer


















                              • 3





                                How does this piece of code explains the issue? Can you provide an explanation of the code you have posted?

                                – Shidersz
                                12 mins ago











                              • They can’t access the inner function value so we have to defined globally. After globally you can use A value anywhere in the code

                                – Darshit Shah
                                10 mins ago















                              -1














                              var a=0;
                              function outer()
                              a=2;
                              function inner()
                              a=a+1;
                              console.log(a)
                              a = 8

                              inner()

                              outer()





                              share|improve this answer


















                              • 3





                                How does this piece of code explains the issue? Can you provide an explanation of the code you have posted?

                                – Shidersz
                                12 mins ago











                              • They can’t access the inner function value so we have to defined globally. After globally you can use A value anywhere in the code

                                – Darshit Shah
                                10 mins ago













                              -1












                              -1








                              -1







                              var a=0;
                              function outer()
                              a=2;
                              function inner()
                              a=a+1;
                              console.log(a)
                              a = 8

                              inner()

                              outer()





                              share|improve this answer













                              var a=0;
                              function outer()
                              a=2;
                              function inner()
                              a=a+1;
                              console.log(a)
                              a = 8

                              inner()

                              outer()






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 19 mins ago









                              Darshit ShahDarshit Shah

                              53




                              53







                              • 3





                                How does this piece of code explains the issue? Can you provide an explanation of the code you have posted?

                                – Shidersz
                                12 mins ago











                              • They can’t access the inner function value so we have to defined globally. After globally you can use A value anywhere in the code

                                – Darshit Shah
                                10 mins ago












                              • 3





                                How does this piece of code explains the issue? Can you provide an explanation of the code you have posted?

                                – Shidersz
                                12 mins ago











                              • They can’t access the inner function value so we have to defined globally. After globally you can use A value anywhere in the code

                                – Darshit Shah
                                10 mins ago







                              3




                              3





                              How does this piece of code explains the issue? Can you provide an explanation of the code you have posted?

                              – Shidersz
                              12 mins ago





                              How does this piece of code explains the issue? Can you provide an explanation of the code you have posted?

                              – Shidersz
                              12 mins ago













                              They can’t access the inner function value so we have to defined globally. After globally you can use A value anywhere in the code

                              – Darshit Shah
                              10 mins ago





                              They can’t access the inner function value so we have to defined globally. After globally you can use A value anywhere in the code

                              – Darshit Shah
                              10 mins ago

















                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55428371%2fwhy-do-variable-in-an-inner-function-return-nan-when-there-is-the-same-variable%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Are there any AGPL-style licences that require source code modifications to be public? Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Force derivative works to be publicAre there any GPL like licenses for Apple App Store?Do you violate the GPL if you provide source code that cannot be compiled?GPL - is it distribution to use libraries in an appliance loaned to customers?Distributing App for free which uses GPL'ed codeModifications of server software under GPL, with web/CLI interfaceDoes using an AGPLv3-licensed library prevent me from dual-licensing my own source code?Can I publish only select code under GPLv3 from a private project?Is there published precedent regarding the scope of covered work that uses AGPL software?If MIT licensed code links to GPL licensed code what should be the license of the resulting binary program?If I use a public API endpoint that has its source code licensed under AGPL in my app, do I need to disclose my source?

                              2013 GY136 Descoberta | Órbita | Referências Menu de navegação«List Of Centaurs and Scattered-Disk Objects»«List of Known Trans-Neptunian Objects»

                              Button changing it's text & action. Good or terrible? The 2019 Stack Overflow Developer Survey Results Are Inchanging text on user mouseoverShould certain functions be “hard to find” for powerusers to discover?Custom liking function - do I need user login?Using different checkbox style for different checkbox behaviorBest Practices: Save and Exit in Software UIInteraction with remote validated formMore efficient UI to progress the user through a complicated process?Designing a popup notice for a gameShould bulk-editing functions be hidden until a table row is selected, or is there a better solution?Is it bad practice to disable (replace) the context menu?