Note also that JavaScript is case-sensitive, i.e. message
is regarded as a different variable name to Message
or MESSAGE
. This is in contrast to Construct's event sheets, which are usually case-insensitive.
It's a good idea to always use descriptive variable names to help make your code easy to understand. There are also different naming conventions, such as whether you use underscores (first_name
) or "camel case" (firstName
). It doesn't matter much which you pick - just be consistent, and if you work in a team, make sure everyone agrees on the same naming style.
Constants
You can also declare a constant, which is like a variable that cannot be modified. These are declared with const
instead of let
. Some values never need to change when your code runs, such as the mathematical constant pi, or the maximum number of login attempts allowed. Trying to change these values should be an error, and const
enforces this.
// Declare a constant
const PI = 3.141592653589793;
// TypeError: Assignment to constant variable.
PI = 4;
The naming rules for constants are identical to variables, but a common convention is to name constants with all uppercase names to help tell them apart from variables.
var
You may come across old JavaScript code using var
to declare variables:
var message = "Hello world!";
You can assume this works the same as let
. Avoid using it though, as it is an old feature with some odd quirks which we won't go in to here. Modern code should only use let
.