Javascript: / Home / OldStyleDeclarations

Old Style Variable Declarations

Up to this point, changes have been made directly to existing objects in the DOM. Often it is better to create a temporary variable that is easier to use.

In script that is not in "strict" mode, a variable can be created by just typing the variable name and assigning it a value.

myVar = "value";

This creates a global variable of type "string" where the value and type can be changed anywhere in the script. It is easy with this syntax to create errors that are hard to find.

A variable can also be created using the "var" keyword.

var myVar = "value";

This variable will belong to the function, class or method where it is defined.

Using "var", the variable value and type can still be redefined somewhere else in the code. As in: var myVar = 5;. Now the variable type is "numeric" which has a different behavior than a type of "string".

<- AddEvent            NewStyleDeclarations ->
9OldStyleDeclarations