UnityScript versus JavaScript

Though many in the Unity development community (and even in the Unity corporation) refer to UnityScript and JavaScript as if they were equivalent or interchangeable, the two are very different languages. Though they resemble each other syntactically, they have very different semantics.

Name
A lot of people, including some in the Unity corporation, like to call UnityScript by the name "JavaScript." I believe this is wrong on several levels.

While "JavaScript" is merely a generic name and could refer to any one of many implementations of the ECMAScript specification, Unity's ".js" language doesn't even come close to conforming to that specification — nor does it try to. UnityScript is a proprietary language, and it doesn't follow any concrete specification; it is modified at the will of the Unity developers.

The vast majority of JavaScript libraries you find will not work just by copying them into Unity. Unity's "JavaScript" is most similar to Microsoft's JScript.NET, although it is not quite identical. Thus, many including myself believe the language Unity uses to be called "UnityScript" instead.

Some people consider this to be "just semantics," but when people call what is properly UnityScript by the name "JavaScript," it becomes a lot harder to search the Internet for solutions and help with UnityScript. It's also quite a lot of trouble to put oneself through to continually specify whether one is referring to "real JavaScript" or "Unity's JavaScript." So, it's best to just stick with "JavaScript" for real JavaScript and "UnityScript" for Unity's language.

This page attempts to explain the differences between JavaScript (ECMAScript) and UnityScript as succinctly and clearly as possible. If you have any suggestions, feel free to add them to the author's talk page.

JavaScript is class-free
JavaScript (i.e. the language that's not UnityScript) has no classes. This is because it's a prototypal language, and not a classical one. Inheritance happens with [more dynamic] objects, rather than [unchanging] classes, in JavaScript.

As shown above, in JavaScript, a function can create an object, when called with the  keyword. After that happens, the prototype (template) object Machine can be extended to provide additional functionality, and all class instances, past and future, are affected by this extension.

UnityScript has classes, unlike JavaScript. Also, in UnityScript, once you've defined a class, that class is more or less fixed for the duration of the runtime of your program. (N.B. There may be some exceptions to this rule, such as Reflection, but you probably do not need this and should not be using it because it's not very efficient.) However, the class system has the added benefit of being an easier-to-read, more familiar (to most) language.

File name matters
UnityScript tries to save you from typing. Most files represent single classes, so automatically, the name of a UnityScript file is used to define a class which the file's contents are assumed to implement.

For instance, the following file Dog.js:

Is essentially interpreted as the following equivalent C# code:

JavaScript just executes code, with no regard to the file the code is stored in.

Semicolons are "more required"
While JavaScript tries to make semicolons unnecessary, the approach it takes, often dubbed "semicolon insertion," is not always perfect. This often leaves JavaScript developers with buggy code and confused minds. So, UnityScript avoids this trouble by simply requiring semicolons and not trying to insert them by itself.

Semicolons are required in several places (pretty much after everything):
 * After,  , or   statements.
 * After expression statements.


 * After variable declarations.
 * After variable assignment.
 * After a bodiless method declaration (such as in an interface).
 * Between the parameters in a  loop.

One variable declaration at a time
JavaScript supports multiple variable declarations in one  statement. UnityScript does not.

Assignment cannot be an expression
In JavaScript, assignment is treated as an expression.

In Unity, assignment is always a statement, so this must be broken up into steps.

The exceptions are the [pre/post]-[in/de]crement operations:

No Global Variables
Every top-level variable in JavaScript is global. Additionally, any variable declaration not preceded by the  statement is automatically scoped to be global. This is not the case in UnityScript; there are not really any global variables in UnityScript per sé.

Dynamic typing can be inefficient
Both UnityScript and Javascript support dynamic typing.

However, in UnityScript, when one uses this feature, it can become inefficient to access x many times. This is because the compiler recognizes that the type of the object may change over time. Therefore, the compiler basically produces what would be produced by the following C# code:

This works just fine. Anything can be cast to an object. But when you want to call a method on x, such as Push:

Basically the compiler doesn't know what type to cast x to. So we have to do it ourselves by replacing the last line with something like, and a lot of casting can make things inefficient.

If we never change the type of x, however, the compiler will recognize this and not use the Object datatype for our variable.

By contrast to all of this, in JavaScript, everything is dynamically typed, so instead of having an inefficient dynamic typing system, everything in JavaScript is inefficient.

Privacy
In JavaScript, privacy is rather unconventional.

In UnityScript, it can be more intuitive.

No Bling
Dollar signs ($) are not allowed in UnityScript identifiers as they are in JS identifiers. (In JS, the symbol is often used for c-style namespacing or as the name of a do-everything function.)

No statement
There is no  statement in UnityScript. This is probably for the best, as JavaScript's  statement causes the whole language to be slower, regardless of whether the statement is used or not. It is also considered harmful.

UnityScript has .NET's OOP features
UnityScript supports classes, as well as "protection levels" (public, private, protected) and "static" keyword options. Since it also supports explicit typing, it also has support for "generics" (runtime type enforcement), which JavaScript has no notion of.

No in UnityScript
JavaScript allows a way for you to remove declared variables from the namespace. UnityScript doesn't.

No Regular Expression Literals (RegExp or regex)
In JavaScript (and even JScript.NET, the language on which UnityScript is based), one can directly define a regular expression using a syntax like the following:

UnityScript does not support this, understandably, as regular expressions are pretty uncommon in game code, and the syntax is hard to tokenize/lex.

The keyword
In JavaScript,  refers to something called the "context". It's basically a variable that's set in a different way than other variables are set, and can have practically any value except for  or.

In UnityScript,  only ever means one thing: basically, the object on which a method is being called.

Note that without the  qualifier, by default, an identifier (e.g.  ) will refer to a class field  UNLESS a variable of the same name has already been defined (e.g. the argument eyeColor). In such a case,  must be explicitly included to disambiguate between the variable name and the field (property) name, as seen on line 7 of the code snippet. When a variable or argument hides the visibility of a field, it is called "shadowing".