Skip to main content

Variables

Variables are declared with const or let, exactly as in standard JavaScript:

let count = 2;
count = 5; // reassignment of a `let` variable

const name = 'techwork'; // a `const` cannot be reassigned

For the general behavior of const and let — block scoping, reassignment rules, and so on — see the MDN documentation on const and let.

warning

JavaScript and AutomatorScript also allow you to declare variables via the var keyword, but this should be avoided, as explained here.

Global variables and the $ prefix

A global variable is one declared in the global scope, outside of any function or block. A local variable is declared inside a block (a set of statements in curly braces {}) and is only visible within that block and blocks nested inside it.

AutomatorScript adds one additional rule of its own: a variable which name starts with $ is always global, no matter where it is declared. In other words, a $-prefixed variable becomes global even when declared inside a block or function.

To make variables available to templates, they must be prefixed with $.

In addition, $-prefixed variables can be used for JSON and SOAP services to store the response data (typically named $result), the status code ($statusCode) and the content type ($contentType).

Environment variables, which are configured in the Environment variables tab, all start with $ and are available as globals in every package.

warning

Declaring global variables inside a function or block can be confusing and may make your packages difficult to understand. We recommend that you use $-prefixed variables only for the mentioned purposes.

Standard variables

Depending on how a package was triggered, certain standard variables are made available it. This is explained in more detail under Standard variables.

Literals and types

The values you assign to variables — objects, arrays, strings, numbers, dates, regular expressions, Map, Set, and so on — use standard JavaScript literal syntax:

let config = { company: 'techwork', since: 1999 };
let ids = [1, 2, 3];
let greeting = `copyright ${config.company}, since ${config.since}`;
let today = new Date();
let pattern = /\w+/;

For the full range of built-in types and the methods available on each, see the Built-in objects reference.