# Variables

```go
let count = 0              // the type is inferred: number
let name string = "Ann"
let total number           // the zero value: 0
const PI = 3.14
let a, b = 1, 2
```

```js
let count = 0
let name = 'Ann'
let total = 0
const PI = 3.14
let a = 1,
  b = 2
```

A `const` cannot be reassigned; the compiler checks that.

Multiple assignment, as in Go:

```go
a, b = b, a                // → [a, b] = [b, a];
```

The fields of an object go into variables as in JS, one level deep, in `const` and `let`:

```go
const { name, size: bytes, ...rest } = file    // rest has the other fields
const { "content-type": mime } = headers
```

Each variable has the type of its field, and a field that the object does not have is an error. A
method is not taken: without its object it would lose `this`. Arrays are unpacked with several
values instead, `const first, second = pair()`.

### Zero values

A variable or a class field without an initial value gets the zero value of its type, as in Go:

| Type                        | Zero value                             |
| --------------------------- | -------------------------------------- |
| `number`                    | `0`                                    |
| `bigint`                    | `0n`                                   |
| `string`                    | `""`                                   |
| `bool`                      | `false`                                |
| `T[]`                       | `[]` (a new array each time)           |
| `byte[]`, `float64[]`, ...  | an empty typed array                   |
| `queue T`                   | an empty queue (a new one)             |
| `chan T`, `topic T`         | `Channel.EMPTY`, `Topic.EMPTY`: closed |
| `?T`, `error`, `any`        | `null`                                 |
| classes, interfaces, `func` | none: the value must be given          |

```go
let u User                 // error: User has no zero value
let u ?User                // fine: u == null
```

### A variable in its own initializer

A variable exists from the start of its declaration, as in JS. A function in its initializer runs
after the initializer, so it can use the variable:

```go
const timer = setInterval(() => {
    if done() {
        clearInterval(timer)
    }
}, 1000)

const fact = func(n number) number {
    return n <= 1 ? 1 : n * fact(n - 1)
}
```

The same holds for event handlers in markup: `const close = <button onClick={close.remove()}>`.

The body of such a function is checked once the variable has its type. That works when the type of
the function is known without its body: a `func` literal, an event handler, or a callback whose
parameter types and result come from the call. Otherwise the type of the variable is written:

```go
const fib = (n number) => fib(n - 1)            // error: give "fib" a type
const fib func(number) number = n => n < 2 ? n : fib(n - 1) + fib(n - 2)
```

A use outside of functions reads the variable before it has a value: in JS that is a
`ReferenceError`, here a compile error. In Go, `x := x + 1` in a nested scope uses the outer `x`;
in Rook, as in JS, it is the new variable, so the compiler asks for another name:

```go
const x = 1
func f() {
    const x = x + 1    // error: "x" here is the new variable, which has no value yet
}
```

The rule is that of JS because the declaration compiles to the same JS declaration; the compiler
only reports at compile time what would fail at run time.

