[JavaScript]
[Previous page] [Section contents page] [Next page]
Variables

Declaring Variables

Variables in JavaScript can be declared in various ways, the most simple being statements such as:

  • var someThing
  • var someThing=2
  • var someThing=2.54
  • var someThing=true
  • var someThing=document.bgColor
  • var someThing="apples"

The keyword var in these statements isn't absolutely required, but as we will see in the discussion of global and local variables below, the keyword may determine the nature of the variable. The most important difference between the first and the last five examples shown above is that in the last five, the variable someThing is initialized (given an initial value). Although it is not strictly required, you should always initialize variables when you declare them. The reason is that using an uninitialized variable in an operation is dangerous, and will often cause the script to fail. If the variable has no obvious initial value (because it is to be determined by some later operation), give it the value null (which means no specific value, but serves to initialize the variable). It is also important to note that JavaScript treats unknown entities it finds in expressions as global, uninitialized variables, which could cause various problems -- make sure you declare your variables explicitly.

Data Types

Variables represent data, and there are five implicit data types in JavaScript:

  • integer
  • floating point
  • boolean (true or false, also interpreted as 1 or 0)
  • "nonatomic" types, such as objects and functions
  • string (any string of text, enclosed in quotation marks)
  • null (a special non-value used for initializing variables)

These types are implicit in that you cannot choose the type -- instead, the program determines the type based on the value you assign to variable, and changes the type as necessary when the value changes, or when the variable is combined with another of a different type. Once you've gotten a lot of experience with JavaScript, you will probably want to dig into the details of data type-switching with some advanced reference. For beginners, however, the safe and sane route is: don't combine variables of different types, or attempt to change the data type (one exception: mixing numbers and strings when you're simply writing data to a page is safe).

Global and Local Variables

All variables declared outside of any function are global, meaning that are available to any statement or function in the script. Variables created within functions are a little more complicated.

Inside functions, variable declarations beginning with the keyword var will create local variables, which are invisible outside the function. If the var is omitted, the variable will be global. In some cases it may not matter, but in others it might. The safest course to avoid confusion is to declare all global variables outside of any function (this is usually done at the beginning of the script), and declare only local variables within functions. If you follow this practice, you can always use the var keyword in your declarations and know that you're getting it right.

[Previous page] [Section contents page] [Next page]