# variables & values

### `val` vs `var`

```csharp
val maxHealth = 20 // immutable (can never be reassigned)
var currentHealth = 20 // mutable (can be reassigned)
```

trying to reassign a `val` is a runtime error:

```csharp
val name = "Steve"
name = "Alex" // error: cannot reassign 'val name': it is immutable
```

use `var` for anything that changes like counters, flags, current game state, etc. use `val` for everything else, or don't, you do you

katton has exactly five kinds of runtime value, plus `null`:

| type | example | notes |
|---|---|---|
| number | `5`, `3.5`, `-2` | no separate int/float, everything is a decimal |
| string | `"hello"` | double quoted only |
| boolean | `true`, `false` | |
| list | `["a", "b", "c"]` | ordered and mutable |
| map | `{ "key": "value" }` | insertion ordered and mutable |
| null | `null` | represents "nothing" |

### assignment operators

```csharp
var score = 0
score = 10 // plain assignment
score += 5 // score = score + 5 -> 15
score -= 3 // score = score - 3 -> 12
score *= 2 // score = score * 2 -> 24
score /= 4 // score = score / 4 -> 6
```

### top level vs local

```csharp
var globalCounter = 0 // top level: shared across every function in this script

function increment() {
    var localTemp = 1 // local: only exists inside this call
    globalCounter += localTemp
}
```

### `null`

```csharp
val item = event.item   // might be null if there's no item involved

if (item != null) {
    // safe to use item here
}
```

always check for `null` before using a value that might not be present: `event.item`, `Katton.pick()` on an empty list, and `Katton.vars.*.get(...)` on an unset key can all return `null`
