Comments
Users can place insert comments anywhere in a script. Comments are not executed like regular code. Instead, they can be used for documentation or to temporarily disable blocks of code.
...
Single-line comments start with
//
and end when the line ends.Block comments start with
/*
and end with*/
. These can extend across multiple lines, or can be used within a single line.
Example:
Code Block | ||
---|---|---|
| ||
// This is a single-line comment /* This is a * multi-line * comment.*/ |
Calling Functions
A function takes in some parameters and returns a value. Functions let you re-use pieces of code without re-writing everything.
...
For example, tag.write
takes two arguments. The first is the tag name, and the second is the value to write. The script below writes the value 1
to the tag called "my_tag"
:
tag.write("my_tag", 1);
Declaring Variables
Variables let you store values and access them later in the script.
...
To declare more than one variable, use a comma ,
between each variables:
var variable_name_1, variable_name_2, ... , variable_name_n;
...
Variables must be declared before they can be used in the script. Variables declared in a separate script cannot be used unless you first import that script using system.importScript
.
Using and Setting Variables
Once a variable has been declared, you can assign a new value to it using the assignment operator =
. For example:
...
Code Block |
---|
tag.write("my_tag", 1); |
Operators and Parentheses
You can add, subtract, multiply, and use other operators on variables and other values. See the “Operators” section for more information.
...
Code Block |
---|
add(1, 2) // 3 add(add(1, 2), 3) // 6 |
Reading and Writing Tag Values
To get a tag’s current value, use tag.read
. This takes in one argument, which is the name of the tag, and returns the value.
...
Code Block |
---|
tag.write("my_tag", tag.read("my_tag" + 1;)) |
Semicolons
JavaScript uses semicolons to indicate the end of expressions. Using semicolons at the end of lines is not required, but is recommended for better organization and clarity:
...
Code Block |
---|
some_function();some_other_function; |
Whitespace
JavaScript ignores whitespace characters like spaces, tabs, and line breaks.
For example, the two scripts below are equivalent:
Code Block | ||
---|---|---|
| ||
var a = 1; if (tag.read("my_tag")) {a=0;} |
Code Block | ||
---|---|---|
| ||
var a = 1; if (tag.read("my_tag")) { a = 0; } |
...