Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Scripting Syntax

Table of Contents
maxLevel4
minLevel1
include
outlinefalse
indent
excludeScripting Syntax
typelist
printabletrue
class

Calling Functions

A function will take in parameters and return a value. Using functions can allow the user to write a piece of code once and reuse it without having to rewrite it multiple times. A function name must be called to use a function, followed by parentheses. Any arguments, if any, should be placed inside the parentheses. Using functions allows the code to retain readability and ease of maintaining the scripts. An example of how to call a script would be:

Code Block
languagejs
page.open("Introduction");

To learn more about the unique functions of Canvas, please refer to the Script and Command Function Appendix.

Semicolons

Due to using Javascript, the Canvas Script Editor will not require semicolons at the end of each line to run. However, using semicolons will increase readability and prevent unexpected behavior in the code:

Code Block
function1();
function2();

This has the same functionality and result as:

Code Block
function1()
function2()

Using semicolons will allow users to have multiple actions on one line in the Script Editor.

Code Block
function1();function2();

Declaring Variables

Variables allow the user to store specific values to be used later within the script. Variable names are case-sensitive, cannot have spaces, and must begin with either a letter or an underscore _. A variable's type will be automatically determined based on what value is used to initialize it.

Variables must be called within the following formats:

Code Block
var variable; // initial value is undefined when no value
// is intialized with the declaration of the variable.

var variable1, variable2, variable3; // multiple variables
// can be declared on the same line separated by a comma.

var variable = 1; // Variables can have their value initialized
// when the variable is first declared.

Variables are local, meaning they can only be used within the script or function in which they are created. No other scripts can access variables from other scripts unless the script is imported.

Using / Setting Variables

When a variable has been declared, users can also initialize a value to be used later with the assignment operator =. Once a variable has been assigned a value, it can also be used to access the value. Here are some examples of the assignment operator being used:

Code Block
var a = 5; // a now has a value of 5
var b = 2; // b now has a value of 2
var tag = "Tagname" // tag now has the value "Tagname" stored to it.

tag.write(tag, a); // this is the same as tag.write("Tagname", 5);

Data types

In Javascript, all variables will have a type associated with it. Boolean, String, and Number are the three most common types that will be used. This type will depend on what is initialized with the variable during runtime. Here are some examples of what the types will look like:

Code Block
43.110   // number
"world"  // string
25       // number
true     // boolean
false    // boolean

Using typeof will also allow the user to check the data type of a variable.

Code Block
var check = 3;
typeof check; // number

Type Coercion

Using multiple data types within one variable can cause something called Type Coercion. This is when the runtime will try to correct which data type the variable could be. Doing this can cause unexpected results, and keeping to one data type per variable at a time is highly recommended. Some examples of what Type Coercion could be is:

Code Block
var a = 5;
var b = "10";
var result = a + b; // result: "510";

!1         // false
275 + true // 276
2 * "25"   // 50

Operators

Operators can be used to add, subtract, multiply, and divide variables. Please refer to the Scripting Basics Appendix to learn more about each operator.

There are three primary ways for operators to be called in Javascript.

Prefix: <operator> <expression 1> Example: var one

Midfix: <expression 1> <operator> <expression 2> Example: A = B

Postfix: <expression 1> <operator> Example: x++

Code Block
1 + 1          // addition, 2
2 - 1          // subtraction, 1
3 * 3          // multiplication, 9
8 / 4          // division, 2
2 == 2         // equal to, true 
2 < 1          // less than, false
!true          // not, false 
true && false  // logical and, false
true || false  // logical or, true

Strings, Quotes, Escape characters

Anything that is in either double quotes or single quotes ' will be treated as a string value no matter what. Even if the name is a variable, if quotes surround it, it will be displayed as what is in the quotes and not the variable's value.

Code Block
var num = 5;
notification.send(num);   // will display 5
notification.send("num"); // will display as num
notification.send('num'); // will display as num 

Using quotes in strings will require an escape character \ or a backslash specifically to use them. Line breaks can also be inserted with \n, while tabbing can be inserted with \t.

Code Block
"\"x\""     // "x"
"\'y\'"     // 'y'
"\\Hello\\" // \Hello\
"Line 1\nLine 2" // Line 1
                 // Line 2
"x\ty\tz"        // x  y  z

Expressions and Parentheses

Expressions can be combined and nested in each other, just like in mathematics. The values will be computed when they are needed. Users can use parentheses to specify the order of operations within the equation. This not only applies to equations but also to function calls. Users can nest functions within other function calls if they will fit the parameters.

Code Block
1 + (2 * 3)  // 7
(1 + 2) * 3  // 9
tag.write("tagName", tag.read("tagName") + 1 ); // adds one to current tagNames value.

Whitespace

Javascript will ignore spaces, tabs, and line breaks.

Scripts with or without whitespace will be the same when they run. The two scripts below are the same and will have the same result when they both run.

Code Block
var x = 2;if(x < 3){x++;} 
Code Block
var x = 2;
if(x < 3){
  x++;
}