Variables can be thought of as named containers. You can place data into these
containers and then simply refer to the data by naming the container.
Before you use a variable in a JavaScript program, you must
declare it. Variables are declared with the var keyword as follows:
<script type="text/javascript">
var xyz;
var abc123;
var stu_dent;
</script>
The scope of a variable is the region of your program in which it
is defined. JavaScript variable will have only two scopes:
- Global
Variables: A global variable has global scope which means it is defined
everywhere in your JavaScript code.
- Local Variables: A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.
- You
should not use any of the JavaScript reserved keyword as variable
name.
- Variable
names should not start with a numeral (0-9). They must begin with a letter
or the underscore character. For example, 123test is an invalid variable
name but _123test is a valid one.
- Variable names are case sensitive. For example, Name and name are two different variables.
The following are reserved words in JavaScript. They cannot be used as variables, functions, methods, loop labels, or any object names.
abstract
boolean break byte case catch char class const continue debugger default delete do double |
else
enum export extends false final finally float for function goto if implements import in |
instanceof
int interface long native new null package private protected public return short static super |
switch
synchronized this throw throws transient true try typeof var void volatile while with |