JavaScript Class

Home Help Table of Contents Class Stuff

Variables

A variable is a "container" for information you want to store. A variable's value can change during the script. You can refer to a variable by name to see its value or to change its value.

Rules for Variable names:

  • Variable names are CASE SENSITIVE
  • They must begin with a letter or the underscore character


Declare a Variable

You can create a variable with the var statement:

var strname = some value

You can also create a variable without the var statement:

strname = some value



Assign a Value to a Variable

You assign a value to a variable like this:

var strname = "Jeremiah"

Or like this:

strname = "Jeremiah"

The variable name is on the left side of the expression and the value you want to assign to the variable is on the right. Now the variable "strname" has the value "Jeremiah".



Local Variables Defined

When you declare a variable within a function, the variable can only be accessed by that function itself. This is called a local variable. The local variable can exist in a different function by the same name.

Global Variables Defined

Variables that are not defined in a function can be accessed from the moment the page loads to the moment it closes. It can also be accessed anywhere else on the page. This is a global variable.





Home Help Table of Contents Class Stuff