Variables are named containers used to store data (values). These can be declared in logo in two ways: let "name value
and make "name value
.
In this example x
, y
and z
are variables declared with the let
keyword:
let "x 5
let "y 6
let "z :x + :y
print :z
In Logo, the " symbol is used to denote a string, such as the name of a variable. The : symbol indicates that you're referencing the value of a variable. In the above example, you would expect x to store the value 5, y to store the value 6, and z to store the value 5 + 6, which is 11.
The keyword make
creates a global variable, meaning it's accessible from your entire program. You can use the keyword let
in the same syntax to create a local variable, one that is only available to the function in which it was declared. To prevent confusion, let
is the preferred method in JSLogo.
Variables can hold strings, numbers, the booleans true
and false
, or arrays of data. Strings are denoted with "; numbers are just numbers; arrays are denoted by containing values in brackets, such as [1 2 3 4]. If you want to check the type of value in a certain variable, you can use the 'typeof' keyword.
make "x 5
print typeof :x
>string
You can apply mathematical operations to values, such as above. All other common operators are supported - addition, subtraction, mulitplaction, division. Strings can be created by wrapping text in single quotes, ''. Values can be operated on with their previous values. Examples below.
let "x 'This is a string'
print :x
>this is a string
let "y 5
let "y :y + 3
print :y
>8
Working With arrays
Arrays, also known as lists, are a type of data structure in which one value contains a list of other values. These are denoted with square brackets, [], with values separated by spaces. They are displayed as a list of values with spaces when printed to the console.
let "array-example [0 1 2]
print :array-example
>0 1 2
You can create an array either by declaring it directly, as in the above example, or by linking together multiple items with the se
operator. se
takes two arguments, the two things to be linked together. Note that JSLogo takes anything declared directly with [] as literal entries; that means numbers and strings will be assumed. Strings don't need quote marks, and variables cannot be used here.
let "firstValue 0
let "secondValue 'apple'
let "list se :firstValue :secondValue
print :list
>0 apple
Existing arrays can have additional entries added to the end by using push
. This is a new feature and is therefore subject to change.
let "test [3 4]
push "test 'apple'
print :test
>3 4 apple
The keyword ``nth` can be used to address individual entries in an array, with the first entry being 0.
let "list [apple orange tangerine]
print nth 1 :list
>orange
Function Name | Arguments | Description |
---|---|---|
make | name (string) value (any) | declare or assign a variable in global scope |
let | name (string) value (any) | declare or assign a variable in local scope |
push | name (string) value (any) | add value to end of existing array with the name of the first argument |
se | value(any) value (any) | returns an array of the two submitted values |
calibrate-list | target source calibration | creates a new array (target) from source array with calibration values applied |
butfirst | array | returns an array without the first item |
butlast | array | returns an array without the last item |
count | variable | returns the length of a variable |
is-defined | variable | returns true or false depending on if the variable exists |