# Optionals

This page: https://emojicode-ide.com/en/docs/basics/optionals/

## There might be a value, or there might not

When you turn a string into a number, or look up a value in a dictionary by key, the operation “might fail” or the value “might not be found.” Results like these are represented by **optionals**. You write the type by putting `🍬` in front, like `🍬🔢`.

No value is represented by `🤷‍♀️`.

```emojicode
🏁 🍇
  🔢 🔤123🔤 10❗️ ➡️ a
  🔢 🔤abc🔤 10❗️ ➡️ b
  ↪️ a 🙌 🤷‍♀️ 🍇
    😀 🔤a has no value🔤❗️
  🍉
  ↪️ b 🙌 🤷‍♀️ 🍇
    😀 🔤b has no value🔤❗️
  🍉
🍉
```

Output:

```text
b has no value
```

## Unwrapping (↪️ … ➡️)

You cannot use an optional in calculations as it is. If you write `↪️ optional ➡️ name 🍇 … 🍉`, the block runs only when there is a value, with the value given that name.

```emojicode
🏁 🍇
  ↪️ 🔢 🔤123🔤 10❗️ ➡️ n 🍇
    😀 🔡 n ✖️ 2❗️❗️
  🍉
  🙅 🍇
    😀 🔤Not a number🔤❗️
  🍉
🍉
```

Output:

```text
246
```

This is the safest way. Even if you forget to write what happens when there is no value in `🙅`, the program will not stop.

## When you know there is a value (🍺)

`🍺 optional` takes the value out directly. However, **if there is no value, the program stops there.** Use it only when you can be sure there is a value.

```emojicode
🏁 🍇
  🍺 🔢 🔤42🔤 10❗️ ➡️ n
  😀 🔡 n ➕ 1❗️❗️
🍉
```

Output:

```text
43
```

## Making your own

If you give a function's (method's) return value or a variable a type with `🍬`, it can hold no value.

```emojicode
🏁 🍇
  🖍🆕 best 🍬🔢
  🤷‍♀️ ➡️ 🖍best
  🔂 n 🍿 3 9 4 🍆 🍇
    ↪️ best ➡️ b 🍇
      ↪️ n ▶️ b 🍇
        n ➡️ 🖍best
      🍉
    🍉
    🙅 🍇
      n ➡️ 🖍best
    🍉
  🍉
  ↪️ best ➡️ b 🍇
    😀 🔤The largest is 🧲🔡 b❗️🧲🔤❗️
  🍉
🍉
```

Output:

```text
The largest is 9
```
