Skip to content

Learn: Variables

Julia Ogris edited this page Jun 20, 2023 · 11 revisions

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.

Declaration

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.

Assignment

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.

Zero Values

After declaration, a variable will have a default or zero value, which is:

Type Zero value
string ""
num 0
bool false

Declaration with Type Inference

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.