Saturday, January 11, 2014

Types of variables in JavaScript

There are 2 types of variables in JavaScript.

1) Local Variable

A variable which is declared inside block or function is called local variable. It is accessible within the function or block only.

Ex:-
<html>
<body>
<script> 
    function mahi()
    { 
        var a=5;//local variable 
    } 
</script>
</body>
</html>

2) Global Variable

A variable which declared outside the function or with window object is known as global variable. It is accessible from any function.

Ex:-
<script> 
       var a=5;//gloabal variable 
      function mahi()
     {  
                 document.write(a); 
      } 
      function piyu()
    { 
              var b=10;//local variable
              document.write(a); 
              document.write(b); 
    }   
 </script>

No comments:

Post a Comment