Why do variable in an inner function return nan when there is the same variable name at the inner function declared after log [duplicate] The Next CEO of Stack Overflow'Hoisted' JavaScript VariablesWhy does shadowed variable evaluate to undefined when defined in outside scope?What 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

Shortening a title without changing its meaning

How can I separate the number from the unit in argument?

Simplify trigonometric expression using trigonometric identities

Why doesn't Shulchan Aruch include the laws of destroying fruit trees?

Is it possible to make a 9x9 table fit within the default margins?

How to show a landlord what we have in savings?

How do I secure a TV wall mount?

Salesforce opportunity stages

Could a dragon use its wings to swim?

Do I need to write [sic] when including a quotation with a number less than 10 that isn't written out?

How should I connect my cat5 cable to connectors having an orange-green line?

Horror film about a man brought out of cryogenic suspension without a soul, around 1990

What day is it again?

Creating a script with console commands

"Eavesdropping" vs "Listen in on"

Read/write a pipe-delimited file line by line with some simple text manipulation

How to pronounce fünf in 45

How can the PCs determine if an item is a phylactery?

Is it okay to majorly distort historical facts while writing a fiction story?

How exploitable/balanced is this homebrew spell: Spell Permanency?

Free fall ellipse or parabola?

Calculate the Mean mean of two numbers

Does Germany produce more waste than the US?

Is the offspring between a demon and a celestial possible? If so what is it called and is it in a book somewhere?



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



The Next CEO of Stack Overflow'Hoisted' JavaScript VariablesWhy does shadowed variable evaluate to undefined when defined in outside scope?What 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










8
















This question already has an answer here:



  • Why does shadowed variable evaluate to undefined when defined in outside scope?

    6 answers



  • 'Hoisted' JavaScript Variables

    3 answers



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















marked as duplicate by Matsemann, Bergi javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
2 days ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






















    8
















    This question already has an answer here:



    • Why does shadowed variable evaluate to undefined when defined in outside scope?

      6 answers



    • 'Hoisted' JavaScript Variables

      3 answers



    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















    marked as duplicate by Matsemann, Bergi javascript
    Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

    StackExchange.ready(function()
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function()
    $hover.showInfoMessage('',
    messageElement: $msg.clone().show(),
    transient: false,
    position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
    dismissable: false,
    relativeToBody: true
    );
    ,
    function()
    StackExchange.helpers.removeMessages();

    );
    );
    );
    2 days ago


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















      8












      8








      8


      1







      This question already has an answer here:



      • Why does shadowed variable evaluate to undefined when defined in outside scope?

        6 answers



      • 'Hoisted' JavaScript Variables

        3 answers



      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

















      This question already has an answer here:



      • Why does shadowed variable evaluate to undefined when defined in outside scope?

        6 answers



      • 'Hoisted' JavaScript Variables

        3 answers



      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





      This question already has an answer here:



      • Why does shadowed variable evaluate to undefined when defined in outside scope?

        6 answers



      • 'Hoisted' JavaScript Variables

        3 answers






      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 2 days ago









      Nick Parsons

      10.4k2926




      10.4k2926










      asked 2 days ago









      ClaudeClaude

      586




      586




      marked as duplicate by Matsemann, Bergi javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      2 days ago


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Matsemann, Bergi javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      2 days ago


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          4 Answers
          4






          active

          oldest

          votes


















          13














          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
































            2














            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






























              0














              Javascript has the mechanism called hoisting .With the help of this mechanism javascript will automatically take all the variable definitions and kept it in the top of the function (whether it may be outer function or inner function). So in your case it will take only the variable definition and hoist it in the top . After hoisting you code will be like this






              function outer() 
              var a = 2;

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

              inner()

              outer()





              you are trying to increment a undefined variable . This is the reason you are getting NaN error.
              So if you initialize the variable after declaration(hoisting) then you will get the result.






              share|improve this answer






























                -2














                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



                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
                  2 days 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
                  2 days ago











                • While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. From Review

                  – double-beep
                  2 days ago

















                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                13














                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





























                  13














                  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



























                    13












                    13








                    13







                    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 2 days ago









                    Shidersz

                    9,4382933




                    9,4382933










                    answered 2 days ago









                    jrojro

                    604215




                    604215























                        2














                        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



























                          2














                          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

























                            2












                            2








                            2







                            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 2 days ago









                            Jack BashfordJack Bashford

                            14.4k31848




                            14.4k31848





















                                0














                                Javascript has the mechanism called hoisting .With the help of this mechanism javascript will automatically take all the variable definitions and kept it in the top of the function (whether it may be outer function or inner function). So in your case it will take only the variable definition and hoist it in the top . After hoisting you code will be like this






                                function outer() 
                                var a = 2;

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

                                inner()

                                outer()





                                you are trying to increment a undefined variable . This is the reason you are getting NaN error.
                                So if you initialize the variable after declaration(hoisting) then you will get the result.






                                share|improve this answer



























                                  0














                                  Javascript has the mechanism called hoisting .With the help of this mechanism javascript will automatically take all the variable definitions and kept it in the top of the function (whether it may be outer function or inner function). So in your case it will take only the variable definition and hoist it in the top . After hoisting you code will be like this






                                  function outer() 
                                  var a = 2;

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

                                  inner()

                                  outer()





                                  you are trying to increment a undefined variable . This is the reason you are getting NaN error.
                                  So if you initialize the variable after declaration(hoisting) then you will get the result.






                                  share|improve this answer

























                                    0












                                    0








                                    0







                                    Javascript has the mechanism called hoisting .With the help of this mechanism javascript will automatically take all the variable definitions and kept it in the top of the function (whether it may be outer function or inner function). So in your case it will take only the variable definition and hoist it in the top . After hoisting you code will be like this






                                    function outer() 
                                    var a = 2;

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

                                    inner()

                                    outer()





                                    you are trying to increment a undefined variable . This is the reason you are getting NaN error.
                                    So if you initialize the variable after declaration(hoisting) then you will get the result.






                                    share|improve this answer













                                    Javascript has the mechanism called hoisting .With the help of this mechanism javascript will automatically take all the variable definitions and kept it in the top of the function (whether it may be outer function or inner function). So in your case it will take only the variable definition and hoist it in the top . After hoisting you code will be like this






                                    function outer() 
                                    var a = 2;

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

                                    inner()

                                    outer()





                                    you are trying to increment a undefined variable . This is the reason you are getting NaN error.
                                    So if you initialize the variable after declaration(hoisting) then you will get the result.






                                    function outer() 
                                    var a = 2;

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

                                    inner()

                                    outer()





                                    function outer() 
                                    var a = 2;

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

                                    inner()

                                    outer()






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered 2 days ago









                                    SuRaSuRa

                                    12114




                                    12114





















                                        -2














                                        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



                                        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
                                          2 days 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
                                          2 days ago











                                        • While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. From Review

                                          – double-beep
                                          2 days ago















                                        -2














                                        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



                                        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
                                          2 days 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
                                          2 days ago











                                        • While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. From Review

                                          – double-beep
                                          2 days ago













                                        -2












                                        -2








                                        -2







                                        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



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

                                        inner()

                                        outer()





                                        share|improve this answer















                                        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



                                        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








                                        edited 2 days ago

























                                        answered 2 days ago









                                        Darshit ShahDarshit Shah

                                        33




                                        33







                                        • 3





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

                                          – Shidersz
                                          2 days 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
                                          2 days ago











                                        • While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. From Review

                                          – double-beep
                                          2 days ago












                                        • 3





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

                                          – Shidersz
                                          2 days 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
                                          2 days ago











                                        • While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. From Review

                                          – double-beep
                                          2 days ago







                                        3




                                        3





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

                                        – Shidersz
                                        2 days ago





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

                                        – Shidersz
                                        2 days 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
                                        2 days 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
                                        2 days ago













                                        While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. From Review

                                        – double-beep
                                        2 days ago





                                        While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. From Review

                                        – double-beep
                                        2 days ago



                                        Popular posts from this blog

                                        Sum ergo cogito? 1 nng

                                        三茅街道4182Guuntc Dn precexpngmageondP