# Declaring Variables

**URL:** https://forum.kirupa.com/t/declaring-variables/265938
**Category:** Uncategorized
**Created:** [July 14, 2008, 7:11pm UTC](https://forum.kirupa.com/t/declaring-variables/265938 "2008-07-14T19:11:30Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![gigaboost](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@gigaboost](https://forum.kirupa.com/u/gigaboost)
#### Post date: [July 14, 2008, 7:11pm UTC](https://forum.kirupa.com/t/declaring-variables/265938/1 "2008-07-14T19:11:30Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)
#### Post date: [July 14, 2008, 7:25pm UTC](https://forum.kirupa.com/t/declaring-variables/265938/2 "2008-07-14T19:25:40Z")

</div>

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).

---

<div class="post-metadata">

### Author: ![gigaboost](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@gigaboost](https://forum.kirupa.com/u/gigaboost)
#### Post date: [July 14, 2008, 7:38pm UTC](https://forum.kirupa.com/t/declaring-variables/265938/3 "2008-07-14T19:38:17Z")

</div>

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

---

<div class="post-metadata">

### Author: ![gigaboost](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@gigaboost](https://forum.kirupa.com/u/gigaboost)
#### Post date: [July 15, 2008, 2:49pm UTC](https://forum.kirupa.com/t/declaring-variables/265938/4 "2008-07-15T14:49:50Z")

</div>

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;

---

<div class="post-metadata">

### Author: ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)
#### Post date: [July 15, 2008, 3:25pm UTC](https://forum.kirupa.com/t/declaring-variables/265938/5 "2008-07-15T15:25:07Z")

</div>

[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.
