Difference of assigning a variable outside any function, in Awake or in Start?

Is

var myVariable = 5;

short for

var myVariable;
function Awake() 
{
   myVariable = 5;
}

? Or is it rather

var myVariable;
function Start() 
{
   myVariable = 5;
}

?

So do the public Variable-Declarations get called only if the script-instance is enabled (-> the same as Start) or are they declared anyway (-> the same as Awake)?

And are there any other merits or flaws of the short or the long versions to be aware of?

Thanks & Greetz, Ky.

Edit 28/07/2011: just found this closely related question

And more on execution-order

In your specific example, the answer is actually neither. It gets compiled to this

public class YourScriptName extends MonoBehaviour
{
    public var myVariable : int = 5;

    // ...then any other functions follow...
}

However, this can differ if your var is initialised as something which cannot be executed outside of a function. In these cases, the value is assigned in the classes constructor. For example, this:

var myVariable = transform.position;

Will be compiled to this:

public class YourScriptName extends MonoBehaviour
{
    public var myVariable : Vector3;

    public YourScriptName()
    {
        myVariable = transform.position;
    }

    // ...then any other functions follow...
}

The class constructor is called during the process of instantiating the class, so these are assigned even before Awake() is called.

"are there any other merits or flaws of the short or the long versions to be aware of?"

In general, it's good to be aware of what the compiler is doing for you behind the scenes, because it can help you understand the system that you're working with better, and ultimately allows you to write more efficient code. There is a little more detail in "Head First into Unity with Javascript" on the wiki, under the heading "Each .js File Implements a Class.

I'm pretty certain they get declared anyways. The "short version" will have the values set BEFORE any functions (including Start or Awake) are called. In addition, if you change the value of myVariable in the editor, with the "long version" the value will get overridden.

As Duck pointed out, the given example goes into the class body. In other words: Variable declarations don't go into any method (methods are what is known as "functions" in JavaScript; in object oriented languages, "methods" is the more common term).

The reason variable declarations outside a "function" in JavaScript go into the class body is because they are actually member variables with object scope, in other words: The variables holding the "state" or "data" of the object. Were they put into a method, they would only be accessible inside that method ("method scope"). This also becomes obvious when you look at such scripts in the Unity editor: Member variables are exposed in the property inspector. You can change them, and those changed values will be available in your scripts.

In other words: One thing that Unity does behind the scenes is serialize and deserialize the values you've entered in the editor.

Executable code you have in a JavaScript file, outside of functions, is put into a Main() method (with the exception that Duck pointed out), so this:

var myVariable = 5;

for (var i = 0; i < 5; i++) {
    myVariable++;
}

Is compiled into this:

[Serializable]
public class TestScript : MonoBehaviour {

    public int myVariable = 5;

    public void Main() {
        for (int i = 0; i < 5; i++) {
            this.myVariable++;
        }
    }

}

The Main() method is executed after Awake() and before Start().

You can deduce some of this from Overview: Writing Scripts in C#, the rest can be found out with a decompiler, like .NET Reflector, and a simple test-class (which prints to Debug from the different methods).