Variables in JavaScript
Construct 2 exports to HTML5, and this means that a lot of the functionality in the game is written in JavaScript. In JavaScript, the type of variable is not defined until you initialize it. While this makes programming in JavaScript much easier, it has its pros and cons. The biggest con is that sometimes you forget which variables are of what type, which leads to unexpected results when you run the app.
Examining JavaScript code
JavaScript is a very popular web language. It is much easier to code in than most other programming languages.
Let's take a look at some examples of JavaScript code.
var score; var timescale = 1.2; var name = "John Bura"; var playerName;
You will notice that all of the variables are simply called var
instead of Int
, Bool
, String
, or Real
. This means that, until you initialize the variable, var
can be whatever you want it to be.
So in this case, score
is not a number—it is simply a variable. However, timescale
is a number...