Section 1: Definition
- A variable is a location in the computer's memory where a value can be stored for use by a program.
- A variable name is any valid identifier.
- Variable names cannot be reserved words and must begin with a letter, underscore (_), or dollar sign ($); subsequent characters can also be letters, digits (0-9), or underscores.
- JavaScript is case-sensitive.
- JavaScript is case-sensitive.
-
JavaScript is case-sensitive.
- I repeated that because it will cause you grief: imgID ≠ imgId
- Begin identifiers with lowercase letters in order to distinguish them from keywords.
- Choose meaningful variable names so that your programs are self-documenting.
Section 2: Declaring Variables
- You should declare a variable with the keyword var.
- var sum; declares a variable named sum.
- If you declare a variable without initializing it, its type is undefined.
- var sum = 0; declares a variable named sum with a numeric data type and an initial value of 0.
- Don't forget the semicolon; they must be used to end each JavaScript statement. clarification
- Variables can be declared in separate statements or in one
statement with variables separated by commas.
- A statement like var loanAmount, interestRate; declares both loanAmount and interestRate.
- Once a variable is defined, do not declare it again in another var statement unless you wish to completely overwrite the variable.
Section 3: Global and Local Variables
- Variables dimensioned outside of a function are called
global variables and are available to every event procedure.
- Global variables come into existence when the program is started and are destroyed when the program terminates.
- Global variables can be used to store information between controls or between multiple clicks of the same button.
- Global variables can cause hard-to-trace program errors.
- Local variables are variables declared in a function, and they can
be referenced only in that function.
- Local variables are created when they are declared in a function, and destroyed when execution of that function terminates.
- Supplement: JavaScript Reference – Variables
Section 4: Initialize all Variables
It is better to initialize your variables, because
- As noted, if you declare a variable without initializing it, its type is undefined.
- some programming languages do not provide default values and uninitialized variables may have random values
- others reading your code do not know whether you wanted to accept the defaults or simply forgot to initialize your variables
- you can initialize variables by assigning it a value immediately
var variableName = "Hello";
var var1 = 14, var2 = 12.50;
Section 5: Constants
Constants are named memory locations whose values cannot change during program execution.
Constants are often used to make a program more readable.
Constants are declared using the keyword const. The constant must be initialized with a constant expression when it is declared and cannot be modified thereafter.
const pi = 3.14159;