Values and Variables
Basic types
Emojicode is a statically typed language: every value has a type. These four are the ones you will use most.
| Type | Meaning | Example values |
|---|---|---|
🔢 |
Integer (64-bit) | 42, -7, 1,000,000, 0x1F |
💯 |
Real number (floating point) | 3.14, -0.5 |
👌 |
Boolean | 👍 (true), 👎 (false) |
🔡 |
String | 🔤Hello🔤 |
You can separate the digits of an integer with ,. A number starting with 0x is hexadecimal.
Variables (constants)
value ➡️ name gives a value a name. A variable created this way is a constant: it cannot be changed later.
🏁 🍇
42 ➡️ answer
😀 🔡 answer❗️❗️
🍉42
Variable names are ordinary alphanumeric names. 😀 can only print strings, so the integer is first turned into a string with 🔡 answer❗️ and then printed (this is covered in detail in Strings).
Trying to put a different value into a constant causes a compile error.
🏁 🍇
42 ➡️ answer
43 ➡️ 🖍answer
🍉Cannot modify constant variable "answer".
Mutable variables
When you want to change a value later, create a mutable variable with ➡️ 🖍🆕 name. To put a new value into it, use ➡️ 🖍name.
🏁 🍇
0 ➡️ 🖍🆕 count
😀 🔡 count❗️❗️
5 ➡️ 🖍count
😀 🔡 count❗️❗️
🍉0 5
It is easier to read if you remember 🖍 as the “rewrite” mark. 🖍🆕 means “create a new variable that can be rewritten,” and 🖍 alone means “rewrite.”
If you create a variable with
🖍🆕and never rewrite it, you get a warning suggesting you make it a constant. It still works, but create things that don't change with➡️ name.
Calculating and reassigning
Combining ⬅️ with an operator gives you a short way to write “calculate with the current value and store the result back.”
🏁 🍇
10 ➡️ 🖍🆕 score
score ⬅️➕ 5
score ⬅️✖️ 2
😀 🔡 score❗️❗️
🍉30
score ⬅️➕ 5 means the same as score ➕ 5 ➡️ 🖍score. Note that ⬅️ on its own cannot be used for assignment.
Declaring first, assigning later
You can also declare a variable first by writing its type, and put a value in it later.
🏁 🍇
🖍🆕 message 🔡
🔤Assigned later🔤 ➡️ 🖍message
😀 message❗️
🍉Assigned later
Arithmetic
| Operator | Meaning |
|---|---|
➕ ➖ ✖️ ➗ |
Add, subtract, multiply, divide |
🚮 |
Remainder of division |
➗ between two integers drops everything after the decimal point. To calculate with real numbers, make one side a 💯.
🏁 🍇
😀 🔡 7 ➗ 2❗️❗️
😀 🔡 7 🚮 2❗️❗️
😀 🔡 7.0 ➗ 2.0 1❗️❗️
😀 🔡 💯 7❗️ ➗ 2.0 1❗️❗️
🍉3 1 3.5 3.5
The 💯 7❗️ in the last line is a call to the method that turns the integer 7 into a real number. The 1 in 🔡 … 1❗️ is how many digits after the decimal point to include in the string.
✖️ and ➗ are calculated before ➕ and ➖. To calculate something first, wrap it in 🤜 and 🤛 (the ( ) of other languages).
🏁 🍇
😀 🔡 1 ➕ 2 ✖️ 3❗️❗️
😀 🔡 🤜1 ➕ 2🤛 ✖️ 3❗️❗️
🍉7 9