There are several key changes you will need to account for when moving from MathJS to JavaScript, including the way you access variables and changes in syntax. This document covers the most common changes.

Where to switch between MathJS and JavaScript

There are two places where you can switch between languages: in the Manage Project Variables window and any places you’re using script expressions in your prototypes or components.

Switching languages in the Manage Project Variables window
 

Switching languages in a script expression

Using MathJS functions inside JavaScript

You can think of JavaScript as a superset of MathJS, in that MathJS is now available inside JavaScript. You can access MathJS functions via the global math object.

Old way in MathJS:

round(3.2)

New way in JavaScript:

math.round(3.2);

Keep in mind that JavaScript has its own global Math object for mathematical operations which you can access via the Math object. Note how it starts with a capital ‘M’.

Calling a method of the native Math class in Javascript:

Math.round(3.2);

 

Accessing project and library variables

The way project and library variables are accessed is different in JavaScript. You now use the $p.  prefix to access project variables, and $l.  prefix to access library variables.

Old way in MathJS:

fullName = concat(firstName, " ", lastName)

New way in JavaScript:

$p.fullName = $p.firstName + " " + $p.lastName;

 

Defining and accessing variables at runtime

New variables can be defined at runtime that can access the current scope – for example the document, statement or function they are created in – just like normal JavaScript.

Old way in MathJS:

myNewVariable = "abc"

New ways in JavaScript:

var myNewVariable1 = "abc";
let myNewVariable2 = "def";
const MY_NEW_CONSTANT = "xyz";

Project variables can also be defined at runtime in script expressions, and accessed by all prototypes & components used in your prototype, as with normal project variables. For example:

$p.myNewProjectVariable = "hi!";

Note: Project variables that you create at runtime won't be available in the user interface and can't be linked to text elements or component states.
 

Accessing event information

Certain types of events – like Blur, Focus, and Keypress events on text inputs – can access an object that holds information relevant to the event. The name of this object has now changed to event .

Old way in MathJS:

ctx.value

New way in JavaScript:

event.value;

 

Commenting

Standard JavaScript single-line and block commenting is now supported. 

Old way in MathJS:

#here's a comment

New ways in JavaScript:

// Here's a single-line comment

/*
  Here's a comment block that
  can span multiple lines
*/

 

Logging to the JavaScript console

Logging to the JavaScript console is now done directly using JavaScript’s console.log function.

Old way in MathJS:

log("yay it works")
log([var1, var2])

New way in JavaScript:

console.log("yay it works");
console.log(var1, var2);

 

Logical operators

Old way in MathJS:

var1 and var2
var1 or var2

New way in JavaScript:

var1 && var2
var1 || var2

  

Converting variables to different data types

Old ways in MathJS:

number(age)
string(taxRate)

New ways in JavaScript:

Number(age);
String(taxRate);
taxRate.toString();

 

Working with arrays

Array indexes in JavaScript are zero-based, whereas in MathJS they start at one. 

Old way in MathJS:

firstValue = myArray[1]

New way in JavaScript:

var firstValue = myArray[0];

 

Variable change conditions

Script expressions in the condition (or “When” field) of a variable change event need to have a return statements. For example:

return $p.name = "Jane";

// or

if($p.name == "Jane") {
  return true;
}

 

See also:

Did this answer your question?