Scripting Basics
Scripting Syntax
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:
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:
function1();
function2();
This has the same functionality and result as:
function1()
function2()
Using semicolons will allow users to have multiple actions on one line in the Script Editor.
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:
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.
Back to Top of Scripting Basics
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:
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:
Using typeof will also allow the user to check the data type of a variable.
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:
Back to Top of Scripting Basics
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++
Strings, Quotes, Escape characters
Anything 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.
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.
Back to Top of Scripting Basics
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 expression. This not only applies to expressions but also to function calls. Users can nest functions within other function calls if they will fit the parameters.
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.
Back to Top of Scripting Basics