documentation/js/js_builtin.md
Load a module plugin.
Parameters
Examples
let serial = require("serial"); // Load "serial" module
Parameters
Examples
delay(500); // Delay for 500ms
Print a message on a screen console.
Parameters The following argument types are supported:
Examples
print("string1", "string2", 123);
Same as print, but output to serial console only, with corresponding log level.
Runs a JS file and returns value from it.
Parameters
Examples
load("/ext/apps/Scripts/script.js");
Convert an ASCII character number to string.
Examples
chr(65); // "A"
Exit JavaScript with given message.
Examples
die("Some error occurred");
Convert a string to number with an optional base.
Examples
parseInt("123"); // 123
parseInt("7b", 16); // 123
Convert a number to string with an optional base.
Examples
let num = 123;
num.toString(); // "123"
num.toString(16); // "0x7b"
Fields
Creates an ArrayBuffer that contains a sub-part of the buffer.
Parameters
Examples
Uint8Array([1, 2, 3]).buffer.slice(0, 1) // ArrayBuffer([1])
Wrappers around ArrayBuffer objects, with dedicated types such as:
Uint8ArrayInt8ArrayUint16ArrayInt16ArrayUint32ArrayInt32ArrayFields
ArrayBufferFields
Removes elements from the array and returns them in a new array.
Parameters
Examples
let arr = [1, 2, 3];
arr.splice(1); // [2, 3]
arr; // [1]
Adds a value to the end of the array.
Examples
let arr = [1, 2];
arr.push(3);
arr; // [1, 2, 3]
Fields
Returns the character code at an index in the string.
Examples
"A".charCodeAt(0) // 65
Same as String.charCodeAt().
Return index of first occurrence of substr within the string or -1 if not found.
Parameters
Examples
"Example".indexOf("amp") // 2
Return a substring between two indices.
Parameters
Examples
"Example".slice(2) // "ample"
Transforms the string to upper case.
Examples
"Example".toUpperCase() // "EXAMPLE"
Transforms the string to lower case.
Examples
"Example".toLowerCase() // "example"
Path to the directory containing the current script.
Examples
print(__dirname); // /ext/apps/Scripts/Examples
Path to the current script file.
Examples
print(__filename); // /ext/apps/Scripts/Examples/path.js
Checks compatibility between the script and the JS SDK that the firmware provides.
Returns
"compatible" if the script and the JS SDK are compatible"firmwareTooOld" if the expected major version is larger than the version of the firmware, or if the expected minor version is larger than the version of the firmware"firmwareTooNew" if the expected major version is lower than the version of the firmwareExamples
sdkCompatibilityStatus(0, 3); // "compatible"
Checks compatibility between the script and the JS SDK that the firmware provides in a boolean fashion.
Examples
isSdkCompatible(0, 3); // true
Asks the user whether to continue executing the script if the versions are not compatible. Does nothing if they are.
Examples
checkSdkCompatibility(0, 3);
Checks whether all of the specified extra features are supported by the interpreter.
Examples
doesSdkSupport(["gui-widget"]); // true
Checks whether all of the specified extra features are supported by the interpreter, asking the user if they want to continue running the script if they're not.
Examples
checkSdkFeatures(["gui-widget"]);