Skip to main content

Example Expressions

Introduction

A collection of example JavaScript expressions.

Expressions

If Statement

  1. Create a JavaScript Utility
  2. Add a string variable called n0
  3. Copy and paste the expression below into the JavaScript Utility.
  4. Create a Value Behaviour.
  5. Connect javascriptUtility.idvalue.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

  1. Create a JavaScript Utility
  2. Copy and paste the expression below into the JavaScript Utility.
  3. Create a Text Shape.
  4. Connect javascriptUtility.idtext.string.
  5. 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();