Skip to content

Learn: Variables

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

Basic Data Types

Evy has three basic data types: string, num, and bool.

  • string: strings are sequences of characters enclosed in quotation marks, such as "Hello World!".
  • num: numbers can be positive, negative, or have decimal points, such as 123.45 or -6.
  • bool: booleans can only be true or false.

Here are some examples of valid and invalid values for each data type:

Type ✅ Valid values ❌ Invalid values
string "abc", "1", "", "\"" 'abc', abc, "abc, "ab"c"
num 123.45, -6, 0.2 +2, .2, "2", 1,000.3, 1 000
bool true, false "true"

Printing Different Data Types

The print function can print all basic data types. For example, the following code will print the string "abc", the number 123, and the boolean value true:

print "abc" 123 true

This will print the following output:

abc 123 true

👉 Note: We cannot always determine the data type of a value by its output alone. For example, the following code will print 1 1 true true:

print "1" 1 "true" true

However, the data types are actually:

  • "1": string
  • 1: num
  • "true": string
  • true: bool

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.

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.

The read command

The read command reads a line of input from the user and returns it as a string. For example, the following code:

s := read
print s s

will read a line of input from the user and print it twice. If you type "hello" and press Enter, the code will print:

hello hello