Wednesday, February 9, 2011

Variables

A variable is a storage locaion in memory with a unique name, like a label on a box that is used to store things.
You create a variable using a js keyword var, and the name of the new variable.

Example:

var + variable name + ;

The var keyword indicates that you're creating a new variable.
The variable name can be just about anything as long as its unique within your script.
The semicolon ends the line of javascript code.

A newly created variable has reserved storage space set aside and it is ready to store data
The key to accessing and manipulating its data is in its name.
That is why its important for the name of every variable to be unique and meaningful!!!


Initialize a variable with "="

Example:

var + variable name + = + initial value + ;

The equals sign connects the variable name to its initial value
The initial value is stored in the variable

var population=300;

This line of script also assigns the data type of the variable as a number because it was given a numeric value, 300. If the variable changes to some other type, then the type of the variable changes to reflect the new data. Most of the time js handles this automatically.

No comments:

Post a Comment