Home
<!DOCTYPE html> <html> <body> <h1>Demo: Global scope</h1> <p id="error"></p> <script> function createUserName() { var userName = "Bill"; } function showUserName() { try{ alert(userName); } catch(ex) { document.getElementById("error").innerHTML = ex; } } createUserName(); showUserName(); // throws error: userName is not defined </script> </body> </html>
Result: