Skip to main content

Variables

A variable is a identifier representing a value. This can be any literal value, a result of a function call, or the result of an expression.

Variables must be declared with const or let before usage.

let x = 2;

let y;
y = 4; // separate declaration and initialization
y = 5; // re-assignment of a variable declared with `let`

let z = x + y; // initializing a variable via an expression

const $a = 2 * z;
const b = 42;

let text = "Text is in double quotes"
let other_text = 'or in single quotes if double quotes are inside like "this"'

More information about const and let can be found in the MDN JavaScript documentation:

Global and local variables

A global variable is a variable declared outside of any function or block, also called the global scope.

Variables that are declared inside a function or block are called local variables. They are only available inside that block, or inside blocks that are nested inside that block.

A block is a set of statements enclosed in curly braces {}.

The following example illustrates the difference between global and local variables.

let a = [1, 2, 3];

function myFunction() {
let b = 2;
log("the value of b is", b);

// a is available here, both because it is a global variable
// and because myFunction() is "nested" inside the global scope
a.forEach(val => {
log(`the value of ${val} + b is`, val + b); // b can be used inside nested blocks
});
}

// a (a global variable) is available here
// b (a local variable) is NOT available here

Global variables in AutomatorScript

To use a variable in a template, it must be declared as a global variable.

In AutomatorScript, it is considered good practice to indicate global variables with a leading $ sign.

Variables defined in the "Environment variables" tab all start with $ and are available in all packages as global variables.

warning

For historical reasons, a variable which name starts with $ has a special meaning in AutomatorScript. It will always become a global variable, no matter whether it is declared inside a block or not.

Also, in AutomatorScript it is optional to use const or let to declare a variable that starts with $. That means that the following code will work:

$a = 3; // (not recommended) You can omit `let` or `const` when declaring `$` variables

However, it is recommended to:

  • avoid declaring global variables inside functions or blocks
  • explicitly declare all global variables with const or let

Literals

This section lists some examples of literals of various types. More information about these types can be found in the Built-in object types section of the Function reference.

Object

let obj = {
company: 'techwork',
since: 1999
};

Array

let array = [1, 2, 3];

Iterator

let iterator = array.entries();

String

let string = "Hello";
let string_value = `copyright by ${obj.company} - registered since ${obj.since}`;

Regular expression

let reg_value = /\w+/;

Math

Math.PI;

Date

let today = new Date();
let since = new Date(1999, 1, 10);

Map

let my_map = new Map();

Set

let my_set = new Set();

Moment

let d = new Date();
let m = moment(d);

Working with lists

Lists can contain kind of data. It can store simple values, but also complex objects. A list is called an Array in JavaScript.

This section introduces the most common usage of methods to manipulate the content of the list. The page about functions contain additional examples.

Basic examples

let list = [10, 20, 30]

log('First element of the list has index 0:', list[0]) // 10
log('First element of the list :', list.first()) // 10
log('Last element of the list :', list.last()) // 30

log('Length of the list', list.length) // 3

// Add Element at the end:
list.push(15)
log('Last element of the list :', list.last()) // 15
log('Length of the list', list.length) // 4

// Add Element at the start of the list
list.unshift(25)
log('First element of the list :', list.first()) // 25
log('Length of the list', list.length) // 5

// Remove Element at the end:
list.pop()
log('Last element of the list :', list.last()) // 30
log('Length of the list', list.length) // 4

// Remove Element at the start:
list.shift()
log('First element of the list :', list.first()) // 10
log('Length of the list', list.length) // 3

// reverse the order of the elements:
list.reverse()
log('First element of the reversed list :', list.first()) // 30

Slice and splice

let animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
let result = null

// Get all elements starting on a specific index
result = animals.slice(2)
log('All elements starting at index 2', result); // ["camel", "duck", "elephant"]

// Get all elements between two index values
result = animals.slice(2, 4)
log('All elements from index 2 to index 4', result); // ["camel", "duck"]

// Remove three items on a specific index
let removed = animals.splice(1, 3)
log('the changed animals list', animals) // ['ant', 'elephant']
log('the removed elements', removed) // ['bison', 'camel', 'duck']

// replace one element on a specific index
let dune_animals = ['bison', 'camel', 'duck']
dune_animals.splice(2, 1, 'lion')
log('the changed dune animals list', dune_animals) // ['bison', 'camel', 'lion']

// insert one element at index 2
dune_animals.splice(2, 0, 'snake')
log('the inserted dune animals list', dune_animals) // ['bison', 'camel', 'lion']

Arrays of objects

let list = [
{ id: 3, subject: 'Running out of coffee' },
{ id: 6, subject: 'Toner is empty' },
{ id: 8, subject: 'Paper needs to be replaced' }
];
let ids = list.map(item => item.id); // [3, 6, 8]