/
13.1.3 Program Structure

13.1.3 Program Structure

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.

JavaScript supports two types of comments:

  • 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:

// This is a single-line comment /* This is a * multi-line * comment.*/

This manual will use comments to indicate the result from pieces of code. For example:

1 + 2 // 3

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.

Functions can be executed by using the function name, followed by parentheses. Any arguments should be placed inside of the parentheses.

For example, the script below calls the function notification.send with the argument "Hello, world!":

notification.send("Hello, world!"); // brings up a notification saying "Hello, world!"

Some functions do not require any arguments:

Other functions require multiple arguments. In this case, arguments should be separated by commas.

For example, tag.write takes two arguments. The first argument is the tag name, and the second argument is the value to write:

Declaring Variables

Variables let you store values and access them later in the script.

Variable names must not contain spaces, and must begin with either a letter or an underscore _. Variable names are case-sensitive.

Variables can be declared using the following format:

var variableName;

To declare more than one variable, use a comma , between each variables:

var variable1, variable2, ... , variablen;

Users can also define an initial value for the variable when declaring it using the following format:

var variableName = 1;

If you do not specify an initial value, then the variable’s value is undefined.

A variable’s data type is determined automatically based on the value used to initialize it.

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:

Once a variable has a value, you can use the variable name to access that value. For example:

This is the same as:

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.

Just like in mathematics, expressions can be combined and nested within each other. The values will be computed only when they are needed.
Parentheses can be used to group expressions together and to specify the order of operations. For example:

You may also nest function calls:

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:

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.

The type of a variable or expression is determined at runtime. If you attempt to use multiple data types in one expression, JavaScript will attempt to convert (coerce) the types so that they are compatible. Coercion can be useful, but it can sometimes lead to unexpected results. The programmer should always be mindful of what data types are expected for a given variable or expression.

Here are some examples of expressions and their types:

Here are some examples of type coercion:

You can use the typeof keyword to check the type of an expression:

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:

In order to use quotes or other special characters within a string, you must use the escape character \ (backslash), followed by the character you want to use. For example:

You can also use escape characters to insert line breaks \n and tabs \t into strings:

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.

To set a tag’s value, use tag.write. This takes in two arguments: (1) the name of the tag and (2) the value to write.

Tags in tag groups can be accessed using the / character. For example:

Note that tag.read and tag.write do not bind the tag to a variable. For example, consider the following script:

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:

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:

This is the same as:

Semicolons allow you to perform multiple actions in one line of code without line breaks:

Whitespace

JavaScript ignores whitespace characters like spaces, tabs, and line breaks.

For example, the two scripts below are equivalent:

 

Related content