> ## Documentation Index
> Fetch the complete documentation index at: https://requestscript.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Variables

> Declare mutable and constant variables, infer their types and understand scope.

Variables store values. Declare them with `var` or `const`, a name, an optional type and an initial value.

```kotlin theme={null}
request Variables {
    var count: int32 = 0     // explicit type
    var name = "Christopher" // type inferred as string
    const limit = 10         // can't be reassigned
}
```

Both `var` and `const` require an initial value. Use `null` if you don't have a value yet:

```kotlin theme={null}
var result: string = null
```

## var

`var` declares a mutable variable. Reassign it with `=`:

```kotlin theme={null}
request Counter {
    var count: int32 = 0
    count = count + 1
    return count // 1
}
```

There's no compound assignment (`+=`) and no increment operator (`++`). Write `count = count + 1` instead.

## const

`const` declares a variable that can't be reassigned:

```kotlin theme={null}
request Constant {
    const limit = 10
    limit = 20 // Error: Cannot assign to 'limit' because it is a const
}
```

`const` only prevents rebinding the name. If a `const` holds an object or list, you can still change its contents:

```kotlin theme={null}
request MutableContents {
    const car = { color: "red" }
    car.color = "blue" // OK: changes a property, not the binding
    car.doors = 4      // OK: adds a new property
    return car         // { "color": "blue", "doors": 4 }
}
```

Contract parameters are `const` variables.

## Type inference

When you leave out the type, it's inferred from the initial value:

| Initial value | Inferred type |
| - | - |
| Whole number that fits in 32 bits, such as `42` | `int32` |
| Larger whole number, such as `3000000000` | `int64` |
| Number with a fractional part, such as `9.99` | `decimal` |
| `"text"` | `string` |
| `true` or `false` | `boolean` |
| `{ ... }` | `object` |
| A list whose elements all have the same type, such as `[1, 2]` | A list of that type, such as `[]int32` |
| An empty list `[]`, or a list of mixed types such as `[1, "a"]` | A list that accepts elements of any type |
| `null` | Unconstrained: accepts any value |

> [!TIP]
>
> Declare the type explicitly when the inferred type is too narrow. For example, `var total = 0` infers `int32`, so assigning `9.99` to it later is an error. Write `var total: decimal = 0` instead.

## Types never change

A variable's type is fixed when it's declared. Every reassignment is checked against that type, with [numeric promotion](/language/types#numeric-promotion) applied:

```kotlin theme={null}
request FixedType {
    var total: decimal = 0
    total = 5      // OK: int32 promoted to decimal
    total = "five" // Error: a string isn't a decimal
}
```

Assignments to object properties and list elements aren't type-checked, because `object` is untyped and list contents can be changed in place:

```kotlin theme={null}
request ElementAssignment {
    const scores: []int32 = [1, 2]
    scores[0] = 10
    return scores // [10, 2]
}
```

## Assignment targets

You can assign to:

* A `var` variable: `name = value`
* An object property: `obj.prop = value`. If the property doesn't exist, it's added.
* A list element: `list[i] = value`. The index must be an integer within the list's bounds.

## Scope

Each `if`, `else if` and `else` body is its own scope. Variables declared inside a body aren't visible after it:

```kotlin theme={null}
request Scope {
    if (true) {
        var inner = 2
    }
    return inner // Error: Unknown variable 'inner'
}
```

To use a value after the `if`, declare the variable before it and assign it inside:

```kotlin theme={null}
request ScopeFixed {
    var label = "small"
    if (true) {
        label = "large"
    }
    return label // "large"
}
```

## No redeclaration or shadowing

A name can only be declared once. This includes shadowing: declaring a name inside an `if` body that already exists in an enclosing scope is an error.

```kotlin theme={null}
request NoShadowing {
    var x = 1
    if (true) {
        var x = 2 // Error: Variable 'x' is already declared and cannot be redeclared
    }
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.