JavaScript scope within nested function - Javascript Function

Javascript examples for Function:Function Nest

Description

JavaScript scope within nested function

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=( function() {//from ww w. ja  v a  2  s  . c o m
    function bar() {
        var x = "outer";
        function foo() {
            console.log(x); // undefined, is not referring to the outerscope x
            // Due the the var hoising next:
            x = 'inner';
            var x;
            console.log(x); // inner
        }
        foo();
    }
    bar();
    });

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials