Variables

A variable is a named storage location which holds some value, like a quantity, the name of a person or a list of order lines. The names of variables are freely chosen by you, the developer. The values of variables are supplied by expressions. Learning to write such expressions is the essence of computer programming.

Variables carry information through the execution of applications. Each step in an application has access to the variables created by the steps preceding it, and each step may create or modify variables which flow downstream to its successors.

A script step may also contain local variables, which exist only in the scope of that particular step. In this document, you will learn how to create and update local variables in script steps.

Declaring variables

To declare a local variable in a script step, use a let statement.

// Example: Declare a local variable named 'size'
// and assign it the text value "medium".
let size = "medium";

// Now, we can refer to 'size' to access the value "medium":
let result = size;

return result; // The returned value will be "medium".

Variable names can contain alphabetical letters, underscores or numbers, but cannot begin with a number.

// Example: these are all valid variable names.
let x2 = 1;
let number_of_items = 2;
let réduction = 0.4;

All variables have a type, delineating the set of values they can hold. The type of a variable is inferred from its initial value. If the initial value is, for example, a number, then the variable is destined to ever only hold numbers. To see the type of a variable in a script that you are editing in the Flow Designer, you can let the mouse cursor hover over the variable's name.

Advanced: Explicit type annotations

Under some circumstances you might need to explicitly specify the type of a variable. For example, if you are creating a variable x with initial value of 1 but will later update it to have the value null, you should declare the variable as a nullable number.

To explicitly specify the type of a variable, add a colon followed by a type name or type expression before the equals sign.

// Example: create a variable 'x'
// which may be assigned either a numeric value or null.
let x: number? = 1;

Updating variables

To update a local variable in a script step, use a set statement. Updating a variable does not change its type. The type of the new value must be compatible with the type of the previous value.

Only local variables, declared in the same script step, can be updated.

// Example: create a variable and then update its value.
let country = "France";
set country = "United Kingdom";
Advanced: Evolving variable types

There are two cases where you can change the type of a variable by updating it: when the variable was declared with initial value null, or when the variable was declared with an initial value of an empty sequence. In those cases, updating the value causes the type of the variable to evolve, making subsequent statements treat the variable as having the new type.

// Example: evolution from null
let item = null;
if rainyWeather:
    set item = "umbrella"; // 'item' has type 'text?' after this
    
// Example: evolution from empty sequence
let xs = [];
set xs = [1, 2, 3]; // 'xs' has type number* after this.

Last updated