JS Global & Local Variable


Global variable

<!DOCTYPE html>
<html>
<head>
    <title>JavaScript</title>
    <script>
        var a = "Yahoo Baba";
        function hello() {
            document.write(a + "<br>");
        }

        hello(); 
        document.write(a); 
    </script>
</head>
<body>
</body>
</html>


Local variable

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript</title>
  <script>
    function hello(){
      var a = "Yahoo Baba";
      document.write(a + "<br>");
    }

    hello();
    document.write(a);
  </script>
</head>
<body>
</body>
</html>