Language Basics

Variables, Conditionals, Comments, For loops, While loops, Structs

This is a collection of basic syntax elements with examples. It includes an introduction to Variables, Conditional Statements, Loops, and Structs.


Local variables Can be defined using the let, const and var keywords; their identifying rules are the same as contextual variables.

  • let declares a local variable (or a parameter) with a lexical block scope. The variable can only be accessed within its definition block and any nested sub-block. This also forbids variable redeclaration within that scope.

  • const behaves as let but will prevent the variable from being reassigned by any side effect operator.

  • var declares a variable whose scope is the whole script and allows redefinition. This behavior is altered by the JexlFeature#setLexical(true) that will enforce a lexical scope for all variables akin to let declaration.

  • Basic declaration: let x;

  • Declaration with assignment: const theAnswer = 42;

  • Invalid declaration: var x.y;

Local variables they take precedence in resolution over contextual variables. When scripts are created with named parameters, those behave as local variables. Local variables can not use ant-style naming, only one identifier.

To learn more about the different variable types visit Primitive Types.

Conditional Expressions

Conditional statements allow you to make decisions in your code based on specific conditions. The most fundamental form of a conditional statement is the if-else statement, which enables you to execute different blocks of code depending on whether a condition is true or false.


var number1 = 5;
var number2 = 7;

if (number1 == number2) {
    // This block of code will execute if number1 is equal to number2
    return("number1 is equal to number2");
} else {
    // This block of code will execute if number1 is not equal to number2
    return("number1 is not equal to number2");


  • The if keyword is followed by a condition enclosed in parentheses.

  • If the condition is true, the code block inside the first set of curly braces is executed.

  • If the condition is false, the code block inside the else set of curly braces is executed.


Example 1: Checking for Empty Values

In this example, we use the empty function to check if a value is empty. If the value is empty, we display a message; otherwise, we display a different message.

var value = "B2Data"; 
if (empty(value)) {
    return "The value is empty.";
} else {
    return "The value is not empty.";

Example 2: Comparing String Sizes

Here, we use the size function to determine the length of a string and compare it to a specific size. Depending on the size of the string, different actions are taken.

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.";

These examples illustrate how if-else statements can be used to make decisions in your code based on conditions. You can adapt this concept to handle various scenarios and create more complex logic as needed.


Just like most modern languages, B2Win Suite Scripting supports single-line and multi-line comments.

// This is a single-line comments

/* This is a comment
   on multiple lines. */


for, while, do/while, continue, break

for loops

Loop through items of an Array, Collection, Map, Iterator or Enumeration, e.g.

for (let item : list) { 
    x = x + item; 
// Where list is a context variable pointing to any iterable structure.

The following syntax using a context variable is also supported:

for (item : list) { 
    x = x + item; 
// Note that the loop variable item is accessible after loop evaluation

Finally, the conventional syntax using a local variable, initial value, loop condition and loop step is supported.

for (let i = 0; i < size(list); ++i) {
    x = x + list[i]; 
// Where list is a local variable pointing to an array-like structure.


var list = [1,2,3,4];
var x = 0;

for(item : list) {
    x = x + item; 
return x; // returns 10

Note: foreach(item in list) is unsupported.

while loops

Loop until a condition is satisfied, e.g.

while (x < 10) { 
    x = x + 2; 

do/while loops

do { 
    x = x + 2; 
} while (x < 10)

Continue: Within loops (do/while/for), allows to skip to the next iteration.

Break: Allows to break from a loop (do/while/for) unconditionally.

Example: Inventory Replenishment with a While Loop

var inventoryLevel = 100;
var reorderThreshold = 30;

// while invetory level is velow reorderThreshold, restock.
while (inventoryLevel < reorderThreshold) {
    // Place an order to restock inventory.
    // Update the inventory level after placing the order.
    inventoryLevel += 100; // Assuming 100 units are ordered at a time.


Structs are fundamental data structures, providing a convenient way to define custom data types. A struct groups related data fields under a single name, allowing you to create complex data structures tailored to your specific needs. This section will introduce you to defining and using structs.

Defining a Struct

A struct is defined using a syntax similar to the following:

var structExample = {
    "field1": value1,
    "field2": value2,
    // more fields
  • structExample: The name of the struct.

  • "field1", "field2", and so on: The field names within the struct. They must be in quotation marks.

  • value1, value2, and so on: The initial values for each field. You can assign fields empty values as well.

You can define as many fields as needed within a struct, and each field can have a different data type.


Let's create a simple struct to represent a Point in a 2D space:

var Point = {
    "x": 0,
    "y": 0

In this example, we've defined a Point struct with two fields, x and y, both initialized to zero.

Accessing Struct Fields

You can access the fields of a struct using dot notation:

var myPoint = Point; // Create an instance of the Point struct
return myPoint.x; // Access the 'x' field
return myPoint.y; // Access the 'y' field

Another way you can access fields of a struct is using the square brackets:

return myPoint["y"]; // Access the 'y' field
return myPoint["y"]; // Access the 'y' field

Modifying Struct Fields

You can also modify the values of struct fields:

myPoint.x = 10; // Set the 'x' field to 10
myPoint.y = 5;  // Set the 'y' field to 5


We'll create a struct to represent employee information:

var Employee = {
    "name": "",
    "employeeID": "",
    "position": "",
    "salary": 0.0

// Create instances of the Employee struct for two employees
var employee1 = Employee; = "Alice Smith";
employee1.employeeID = "E12345";
employee1.position = "Software Engineer";
employee1.salary = 75000.00;

var employee2 = Employee; = "Bob Johnson";
employee2.employeeID = "E67890";
employee2.position = "Sales Manager";
employee2.salary = 90000.00;

// Calculate the total salary for the two employees
var totalSalary = employee1.salary + employee2.salary;

// Display employee information and total salary
var employeeInfo1 = "Employee 1:\nName: " + + "\nEmployee ID: " +
     employee1.employeeID + "\nPosition: " + employee1.position 
     + "\nSalary: $" + employee1.salary;

var employeeInfo2 = "Employee 2:\nName: " + + "\nEmployee ID: " +
     employee2.employeeID + "\nPosition: " + employee2.position 
     + "\nSalary: $" + employee2.salary;

var combinedInfo = employeeInfo1 + "\n\n" + employeeInfo2 
                + "\n\nTotal Salary for Both Employees: $" + totalSalary;

return combinedInfo;