> For the complete documentation index, see [llms.txt](https://docs.ninox.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ninox.com/ninox-scripting/automate-your-workflows/explore-core-scripting-elements/variables.md).

# Variables

Variables help you store a value and reuse it later in your logic. They make scripts easier to read, test, and update.\
Think of a variable as a named container for a value. You put something in once, then use it wherever you need it. A variable has a name and a value.

In this chapter, you will learn how to:

* declare a variable with `let`
* assign a value with `:=`
* choose a valid variable name

## **Declare a variable**

Use `let` to create a new variable. Use `:=` to assign a value to it.

The basic pattern looks like this:

```ninox
let variableName := value
```

Let's take a look at an example:

```ninox
let myText := "Hello " + first_name + "!";
myText
```

This example declares the variable `myText` and stores a text value in it.

* `"Hello "` is fixed text.
* `first_name` reads the value from the field.
* `"!"` adds the closing punctuation.
* The second line returns the value stored in `myText`.

If `first_name` contains `Sam`, the script returns "Hello Sam!".

Variables only exist while the current logic runs. They do not create a new field in your table. Ninox determines the value type from the value you assign.

{% hint style="info" %}
Use a variable for temporary values inside logic. Use a field when the value should be saved in the record.
{% endhint %}

### **What a variable can hold**

A variable can hold different kinds of values.

In Ninox, common value types are:

* **Text** values such as names or messages
* **Number** values such as prices, quantities, or totals
* **Yes/No** values such as `true` and `false` but also `null`
* **Date** values
* **Record** references

```ninox
let customerName := "Sam";
let total := 125.50;
let isPaid := true
```

This example shows three different value types:

* `customerName` stores text.
* `total` stores a number.
* `isPaid` stores a Yes/No value.

You do not need to declare the value type separately. For example, a text expression returns text, and a calculation returns a number.

### **Why variables are useful**

Variables are useful when you want to store a value once and use it again later. That makes your logic easier to read and easier to change.

For example, you can store a greeting in a variable and then use it in the next line:

```ninox
let myText := "Hello " + first_name + "!";
alert(myText)
```

This example builds the greeting once and stores it in `myText`.

* The first line creates the text.
* The second line uses the stored value.

Without a variable, you would need to repeat the full expression every time.

Variables are also helpful when you use a field from the current record in a search. In that case, store the field value in a variable first. Then use the variable in the condition.

```ninox
let myName := first_name;
let myCount := cnt(select Contacts where first_name = myName);
alert(myCount + " records have the same first name.")
```

This logic works in three steps:

* `myName` stores the value from the current record.
* `select Contacts where first_name = myName` finds matching records.
* `cnt(...)` counts those records.

If the current record has `Sam` in `first_name`, Ninox counts all records in `Contacts` where `first_name` is also `Sam`.

Variables are especially useful when you:

* calculate a value once and reuse it
* build a text once and show it in more than one place
* use a field value from the current record inside a search condition
* split longer logic into smaller, clearer steps

### **Reuse a calculated value**

Variables can store numbers as well as text.

```ninox
let total := price * quantity;
let message := "Total: " + text(total);
alert(message)
```

This example calculates a total and stores it before showing the result.

* `total` stores the numeric result of `price * quantity`.
* `message` turns that value into readable text.
* `alert(message)` shows the final message.

This pattern makes your logic easier to extend later. For example, you can reuse `total` for a discount, tax, or status check.

### **Choose a valid variable name**

Choose a name that clearly describes the value stored in the variable.

Do not use a Ninox keyword as a variable name. For example, `this` and `let` are reserved words, so they cannot be used as variable names:

```ninox
let this := 1
```

Keywords are words that Ninox already uses in its own syntax. Common examples are `let`, `if`, `then`, `else`, `this`, and `end`.

Also, avoid using names that match field names in any tables your script uses. This can lead to wrong results or accidental field updates.

If it helps, use a naming pattern such as `myTotal` or `customerName`. This makes it easier to recognize variables at a glance.

Use clear names such as:

* `myText`
* `totalAmount`
* `customerName`
* `isValid`

### **Patterns to avoid when using variables**

These troublemakers are common when you start working with variables:

* using a keyword or a field name as the variable name
* choosing a name like `x` or `temp` that says nothing about the value
* repeating the same expression instead of storing it once

#### **Choosing names that are too vague**

Short names are fast to type, but they are harder to understand later.

Compare these two versions:

```ninox
let x := price * quantity
```

```ninox
let myTotal := price * quantity
```

The second version is easier to understand. `x` does not tell you what the value means. `myTotal` tells you right away that the variable stores a total. That makes future changes faster and safer.

#### **Repeating the same expression**

If you use the same calculation more than once, store it in a variable. That keeps the logic shorter and easier to change.

Compare these two versions:

```ninox
if price * quantity > 100 then
	alert("High value")
end;
"Total: " + text(price * quantity)
```

```ninox
let myTotal := price * quantity;
if myTotal > 100 then
	alert("High value")
end;
"Total: " + text(myTotal)
```

The second version is easier to follow.

* `myTotal` stores the calculation once.
* The condition is shorter.
* The final line is easier to read.

If you change the calculation later, you only change it in one place.


---

# 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.ninox.com/ninox-scripting/automate-your-workflows/explore-core-scripting-elements/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.
