Declaring Variables

What is the difference between declaring a variable like this:

variable = 1;

and declaring it like this:

var variable:Number = 1?

Why is it better to use the second way?

The use of var is a more standard way of declaring variables. It allows you to explicitly define local variables in functions and is needed to declare class variables in ActionScript 2. Though the use of var is not required in AS1/AS2 for general timeline variables, it is in ActionScript 3. So using var now can potentially make your code more portable should it ever need to go to AS3. Also, the use of var makes it clear that you are declaring a new variable and not using something that already exists, so it also helps in terms of readability.

With the Number type, you’re indicating that the variable made can only have a value whose type is Number. This allows the compiler to do its job better by providing errors if you ever try to use something else as a value for that variable - something non-Number that is. This also has its uses for clarity showing to other people reading the code that a number is used there and nothing else. Again, with AS3 typing also helps with speed improvements, so moving forward its always a good idea to use a type (getting in the habit now will help later on).

Thanks very much for the in-depth answer, appreciated.

A follow up question to my own thread, how do you declare _global. or this. when using this method?

Normally I would just do
_global.MyGlobalVariable = 10;

[QUOTE=gigaboost;2358997]A follow up question to my own thread, how do you declare _global. or this. when using this method?

Normally I would just do
_global.MyGlobalVariable = 10;[/QUOTE]

var nor typing can be used with object-defined variables; that is, values being added directly to an object reference such as this or _global. You can only use your format above.