|
| UNDERSTANDING JAVASCRIPT VARIABLES
Like other programming languages, variables play an integral part in javascript programming. Variables are containers that store information that be later used in the script.
In javascript, variables can be any combination of letters, numbers and the underscore character. However, the variable name cannot start with a number. Variable names are case sensitive. You also cannot use names that are part of the reserved words in javascript syntax. See here for more information.
The following lists some valid variable names:
username
username34
username_34
U2SERNAME_2
The following lists some invalid variable names:
45username
65_username
To create a variable, you give it a name and assign a value to it. This process is often referred to as declaring a variable.
username="dalton56";
If you are declaring a variable inside a function (local variable), and there exists a global variable (outside the function) with the same name, then you must use the keyword var before declaring this local variable as follows:
var username="dalton56";
You should also note that:
If you declare a variable inside a function, this variable is considered a local variable and it's value can only be accessed in that function.
If you declare a variable outside of any function, this variable is considered a global variable and it's value can be accessed by all the functions on your page.
 |
|
|