How to Declare a Variable in JavaScript
Things You'll Need
Instructions
Declare a Variable in JavaScript
1Choose a variable name which meets JavaScript guidelines. Variables names can contain only numbers, letters, or underscores, and must start with a letter or underscore. The are case-sensitive: the variable
ABC
is distinct from abc
.2
Find the best place to declare your variable. Variables declared outside of a function are available to all commands throughout the entire script, and are called "global" while variables exclusive to a specific function are "local", declared inside the function.
3
Declare the variable using the var command, as follows:
var my_variable;
4
Add additional variables to the declaration for compactness:
var one_var, two_var;
5
Specify an initial value for the variable by following it with an equal sign and the value:
var zero=0;
Source...