Comments
Users can 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.
...
indicates that the result of the expression 1 + 2
is 3
.
Calling Functions
A function takes in some parameters and returns a value. This lets you re-use pieces of code without re-writing everything.
...
For example, tag.write
takes two arguments. The first argument is the tag name, and the second argument is the value to write:
Code Block |
---|
tag.write("my_tagmyTag", true); writes the value true to the tag named "my_tagmyTag" |
Declaring Variables
Variables let you store values and access them later in the script.
...
Variables can be declared using the following format:
var variable_namevariableName;
To declare more than one variable, use a comma ,
between each variables:
var variable_name_1, variable_name_2variable1, variable2, ... , variable_name_nvariablen;
Users can also define an initial value for the variable when declaring it using the following format:
var variable_name variableName = 1;
If you do not specify an initial value, then the variable’s value is undefined
.
...
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 |
---|
var tag_name = "my_tagmyTag"; var value = 1; tag.write(tag_nametagName, value); |
This is the same as:
Code Block |
---|
tag.write("my_tagmyTag", 1); |
Operators, Expressions, and Parentheses
You can add, subtract, multiply, and use other operators on variables and other values. See the “Operators” section for more information.
...
You may also nest function calls:
Code Block |
---|
tag.write("my_tagmyTag", !tag.read("my_tagmyTag")) // toggles the value of the tag named "my_tagmyTag" |
It is worth noting that the comparison operator ==
is different from the assignment operator =
. The comparison operator ==
compares two values and returns true
if they are equal, and returns false
if they are not equal. The assignment operator =
sets the variable on the left side to the value on the right side, and always returns assigned value. For example:
Code Block |
---|
var x = 3; x == 5; // false (because 3 does not equal 5) x = 5; // 5 (because 5 is the value being assigned) x == 5; // true (because we set x to 5 in the line above) |
Data Types
In JavaScript, every expression and variable has a data type. The most basic types are Boolean
, Number
, and String
, which can be used to read and write tag values.
...
Code Block |
---|
var x = 3; typeof x; // "number" |
Strings, Quotes, and Escape Characters
Anything wrapped in either double quotes "
or single quotes '
will be treated as a string value. For example, x
without quotes would refer to the variable x. But "x"
or 'x'
would be a string with the value "x". For example:
...
Code Block |
---|
"Line 1\nLine 2"; // Line 1 // Line 2 "1\t2\t3"; // 1 2 3 |
Reading and Writing Tag Values
To get a tag’s current value, use tag.read
. This function has one argument, which is the name of the tag, and returns the tag’s value.
Code Block |
---|
var my_valmyVal = tag.read(“my_tag““myTag“); // assigns the value of the tag "my_tagmyTag" to the variable my_valmyVal |
To write set a tag tag’s value, use tag.write
. This takes in two arguments: (1) the name of the tag and (2) the value to write. tag. write always returns undefined
.
Code Block |
---|
tag.write(“my_tag”“myTag”, 1); // writes the value 1 to the tag "my_tagmyTag" |
Tags within in tag groups can be accessed using the /
character. For example:
Code Block |
---|
tag.write("outer_group/inner_group/my_tagouterGroup/innerGroup/myTag", 1); |
...
Note that tag.read
and tag.write
do not bind the tag to a variable. For example, consider the following script:
Code Block |
---|
var value = tag.read("myTag");
// do some stuff that takes a long time
value = value + 1; |
This would not affect the tag “myTag”, and value
on line 3 might not be using the most recent tag value.
Each time you want to get the most recent tag value, you must use tag.read
, and each time you want to write to the tag, you must use tag.write
. To fix the example above:
Code Block |
---|
var value = tag.read("myTag");
// do some stuff that takes a long time
value = tag.read("myTag"); // get the most recent tag value
value = value + 1;
tag.write("myTag", value); // write the script value to the tag |
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.
...