Language Basics
This page covers the core syntax elements of the B2Win Suite scripting language: Variables, Conditionals, Comments, Loops, Error Handling, and Structs.
Variables
Local variables are defined using the let, const, or var keywords.
let— Block-scoped variable. Cannot be redeclared within the same scope.const— Block-scoped constant. Cannot be reassigned after initialization.var— Script-scoped variable. Allows redeclaration.
let x; // Declaration
const theAnswer = 42; // Declaration with assignment
var name = "B2Win Suite"; // Script-scoped variable
Local variables take precedence over contextual variables. Named parameters also behave as local variables.
For details on data types, see Primitive Types.
Conditional Expressions
Use if-else statements to execute different code paths based on a condition.
var number1 = 5;
var number2 = 7;
if (number1 == number2) {
return "number1 is equal to number2";
} else {
return "number1 is not equal to number2";
}
- The
ifkeyword is followed by a condition in parentheses. - If the condition is
true, the first block executes. - If
false, theelseblock executes.
Examples
Check for empty values:
var value = "B2Win Suite";
if (empty(value)) {
return "The value is empty.";
} else {
return "The value is not empty.";
}
Compare string length:
var text = "Hello, World!";
var requiredSize = 10;
if (size(text) > requiredSize) {
return "The string is longer than the required size.";
} else {
return "The string is not longer than the required size.";
}
Comments
Single-line and multi-line comments are supported.
// This is a single-line comment
/* This is a comment
on multiple lines. */
Loops
for loops
Iterate over arrays, collections, maps, or other iterable structures.
For-each syntax:
for (let item : list) {
x = x + item;
}
Index-based syntax:
for (let i = 0; i < size(list); ++i) {
x = x + list[i];
}
Example:
var list = [1, 2, 3, 4];
var x = 0;
for (item : list) {
x = x + item;
}
return x; // returns 10
foreach(item in list) is not supported.
while loops
while (x < 10) {
x = x + 2;
}
do/while loops
do {
x = x + 2;
} while (x < 10)
continue — Skip to the next iteration.
break — Exit the loop immediately.
Error Handling
Use try, catch, and finally blocks to handle exceptions at runtime.
try {
throw Error("Something went wrong");
} catch (e) {
Logger.error("Error while eval", e.getValue());
} finally {
Logger.info("Cleanup complete");
}
try— Wrap code that may throw an exception.catch— Handle the exception. Usee.getValue()to retrieve the error message.finally— Always executes, whether or not an exception was thrown.
Structs
Structs group related fields under a single name, letting you define custom data structures.
Defining a Struct
var Employee = {
"name": "",
"employeeID": "",
"position": "",
"salary": 0.0
}
Field names must be in quotation marks.
Accessing Fields
var myPoint = { "x": 0, "y": 0 };
return myPoint.x; // Dot notation
return myPoint["y"]; // Bracket notation
Modifying Fields
myPoint.x = 10;
myPoint.y = 5;
Example
var Employee = {
"name": "",
"employeeID": "",
"position": "",
"salary": 0.0
}
var employee1 = Employee;
employee1.name = "Alice Smith";
employee1.employeeID = "E12345";
employee1.position = "Software Engineer";
employee1.salary = 75000.00;
var employee2 = Employee;
employee2.name = "Bob Johnson";
employee2.employeeID = "E67890";
employee2.position = "Sales Manager";
employee2.salary = 90000.00;
var totalSalary = employee1.salary + employee2.salary;
var combinedInfo = "Employee 1: " + employee1.name + " (" + employee1.position + ")\n"
+ "Employee 2: " + employee2.name + " (" + employee2.position + ")\n"
+ "Total Salary: $" + totalSalary;
return combinedInfo;