Variables

Learn how to declare variables, store values, and reuse results in Ninox logic.

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:

let variableName := value

Let's take a look at an example:

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.

Use a variable for temporary values inside logic. Use a field when the value should be saved in the record.

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

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:

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.

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.

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:

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:

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:

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.

Last updated

Was this helpful?