Example Expressions
Introduction
A collection of example JavaScript expressions.
Expressions
If Statement
- Create a JavaScript Utility
- Add a string variable called
n0
- Copy and paste the expression below into the JavaScript Utility.
- Create a Value Behaviour.
- Connect javascriptUtility.id→value.value.
The JavaScript Utility will output 0 if the string in n0
is Text1
, 1 if it is Text2
or 2 if it anything else.
function getIndex() {
if (n0 == "Text1") {
return 0;
} else if (n0 == "Text2") {
return 1;
} else {
return 2;
}
}
getIndex();
Split a word into letters
- Create a JavaScript Utility
- Copy and paste the expression below into the JavaScript Utility.
- Create a Text Shape.
- Connect javascriptUtility.id→text.string.
- Add the Text Shape to a Duplicator.
function getString(n) {
let string = "Cavalry";
let array = Array.from(string);
if (ctx.index < array.length) {
return array[ctx.index];
} else {
return "";
}
}
getString();