·  JavaScript – Syntax – Variables

 

- Info:

  - When you create variable in JavaScript you don't need to define type of data that will be stored in it.

  - Global variables are visible everywhere, outside functions and in all functions.

  - Local variables are only visible inside a function in which they are declared using keyword var.

  - Curly brackets have no influence on variable's scope like they have for instance in JAVA.

 

- Keyword var:

  - Keyword var is used to declare local variable inside a function, similar to keyword my in PERL.

  - If enywhere inside a function variable is declared using keyword var then variable is local for that function.

  - In all other cases variable is global.

  - This means that if no where inside a function variable is declared using keyword var then variable is global.

  - All variables created outside functions are global no metter if keyword var was used or not.

 

    Test.html

    <script type="text/javascript" language="javascript">

   

      test(); 

      function test() {

         b = 10;         //b is global if next line is commented.

         //var b;        //Uncommenting this, b becomes local.

      } 

   

      alert(b);                     

      

    </script>