and a named reference to this memory location is called a variable. Any identifier is
declared as a variable by preceding it with a keyword
var. A
sample declaration of a
variable is:
var result;
This statement declares a variable
result. You may also define the variable as you declare
it using a statement like this:
var result = 0;
or
var result = 23*4+6;
Certain rules have to be followed while naming variables. These rules are as follows:
A variable name can be a combination of numbers and characters.
A variable name cannot start with a number.
The only special characters allowed in variable names is underscore (_)
and dollar
sign($).
There should not be any whitespace in the variable name. For example, ‘result
value’ is not allowed.
JavaScript keywords are reserved and cannot be used.
Please note that JavaScript, unlike HTML is case sensitive. Therefore
VAL and val are two
different variables. Also, it is recommended that the variable name should be such that it
describes the purpose for which it is being used. For example,
if we name a variable result,
it is evident that this variable will contain the result value computed by the code.
Another convention used in JavaScript is to name variables such that the first letter of the
variable name is lowercase. However, every subsequent word in variable name starts with
a capital letter. An example
of this is the variable name, arrayResult. Besides this, the use
of underscore and dollar sign is discouraged. However, they are used in jQuery objects.
Do'stlaringiz bilan baham: