> For the complete documentation index, see [llms.txt](https://docs.luaplusplus.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.luaplusplus.org/language-improvements/constant-variables.md).

# Constant Variables

Constant variables are variables that are immutable after their first assignment, meaning they can **never** be changed. While they provide no performance benefit, as the compiler will propagate unchanged variables, constant variables are a safety feature for large projects where other developers might not realize that the author meant for the variable not to change. Here is a simple example of constant variables in action:

```lua
const a: number = 12
print(a)

a = 1     -- compiler error!
```

## Constant Functions

Constant functions serve a similar purpose as constant variables do but have a slightly different syntax. If you have a function that performs a specific calculation and you don’t want to create a new one, you can do the following in Lua++:

```typescript
const function add(x, y): number ->
  return x + y
  
print(add(1, 2))
```

With this syntax you are telling the compiler that you don’t need an entirely new environment and that the content of this function can be replaced with all the references in your code. Just like constant variables, the compiler can already identify whether functions like this are defined as constant or not, so from a performance standpoint, it doesn’t matter, but as a developer, this can make your code much cleaner and easier to understand.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.luaplusplus.org/language-improvements/constant-variables.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
