-
Notifications
You must be signed in to change notification settings - Fork 9
Learn: Variables
Variables are names or placeholders that store values. A variable must have a data type, which determines the type of value that it can hold.
To declare a variable, you specify its name and data type separated by a colon :
. For example, the following declaration declares a variable named s
of type string
:
s:string
👉 Note: Variable names can contain letters, numbers, and underscores, but they cannot start with a number. It is also a good practice to use descriptive variable names so that you can easily understand what they represent.
Once a variable has been declared, you can assign it a value. To do this, use the equal sign =
. For example, the following assignment assigns the string "Hello, World"
to the variable s
:
s = "Hello, World"
👉 Note: You can only assign a value to a variable after it has been declared.
After declaration, a variable will have a default or zero value, which is:
Type | Zero value |
---|---|
string |
"" |
num |
0 |
bool |
false |
Often, we want to declare a variable and assign it a value directly after.
This can be done using declaration with type inference. In this form,
the type of the variable is inferred from its value. For example, the
following declares a variable named s
and assigns it the value "💥"
:
s := "💥"
The type of the variable s
is inferred to be string
.
Overview | About | Playground | Gallery