Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
var result = (function() {
    function f() {
        return 'foo';
    }
    return (function() {
        // JavaScript does not have block scope, only function scope. So the function below gets hoisted to the top of the function.
        if (1 === 0) {
            function f() {
                return 'bar';
            }
        }
        // Now the function that gets called is the one within this environment, so "bar" gets returned. 
        return f();
    })();
})();
console.log(result); // "bar"
// Demonstration of the hoisting that happens behind the scenes:
var result = (function() {
    function f() {
        return 'foo';
    }
    return (function() {
        function f() {
          return 'bar';
        }
        if (1 === 0) {
        }
        return f();
    })();
})();
console.log(result); // "bar"
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
miguelmotapro
0viewers