Wednesday 16 November 2011

Variable in Javascript

Variable is a storage area where the values can be stored. These values may be a fixed value or the value which is inputted by the user.

·         Declaration of variable

      TO declare the variable in javascript is given below.

            var variablename=value;


            Example
                        var str=”Hello World”;


Where, var is the keyword for defining the variable. This keyword var is not compulsory. A variable can be defined without using var keyword.

There are some Rules for naming the variable which is required to follow them, are as below.

1.      The first letter of the variable-name should be alphabet.
2.      The variable-name can consist of digits or alphabet.
3.      There should be no space between the variable-name.
4.      No special symbol other than underscore (_) is used.
5.      Variable-name cannot be keyword.
6.      The length of variable-name maximum is 31 characters.

·         Life time of variable

When the variable is declared in the function, the lifetime of the variable is for that function only. It cannot be accessed outside the function and so such variable is known as local variable.

When the variable is declared outside the function the lifetime of the variable is for all the functions. It can be accessed by all the function, and so such variable is known as global variable.

No comments:

Post a Comment