Skip to main content

Getting Started

Before diving into the more advanced features, let's start with some basic examples to get you familiar with B2Win Suite Scripting.

Example 1: Hello, World!

A classic "Hello, World!" example to display a greeting message:

var message = "Hello, World!"
return message;

You will be able to see the returned value in real-time in the evaluation section below your code.

Note: By default, in the absence of an explicit return statement, scripts return the value of the last evaluated statement.

Using the return keyword, a script will return the expression that follows (or null).


Example 2: Counting Down to Christmas

This example calculates the number of days remaining until the next Christmas Day by using the now and of methods from the LocalDate class, together with the between method from the Duration class:

var currentDate = LocalDate.now();
var christmasDay = LocalDate.of(currentDate.getYear(), 12, 25);

if (currentDate.isAfter(christmasDay)) {
christmasDay = LocalDate.of(currentDate.getYear() + 1, 12, 25);
}

var days = Duration.between(currentDate.atStartOfDay(),
christmasDay.atStartOfDay()).toDays();
return days; // Returns the number of days until the next Christmas Day.

This example returns the number of days remaining until the next Christmas Day.


Example 3: Simple Arithmetic

You can perform basic arithmetic operations, like addition, subtraction, multiplication, and division. Here's an example:

var num1 = 10;
var num2 = 5;

var sum = num1 + num2;
var difference = num1 - num2;
var product = num1 * num2;
var quotient = num1 / num2;

return [sum, difference, product, quotient];
// returns an Array: [15, 5, 50, 2]

This example showcases how to perform simple math operations and return the results.


Example 4: Working with Strings

You can use and manipulate strings. Here's an example that combines strings:

var firstName = "B2Win";
var lastName = "Suite";

var fullName = firstName + " " + lastName;

return fullName; // returns B2Win Suite

This example demonstrates string concatenation to create a full name.


Example 5: Conditional Statements

Here's an example that checks if a number is even or odd:

var number = 8;
var isEven = number % 2 == 0;

if (isEven) {
return "The number is even.";
} else {
return "The number is odd.";
}

Example 6: Looping

Here's an example of a for loop to print numbers from 1 to 4:

var sum = 0;
for (var i = 1; i < 5; i++) {
sum = sum + i;
}
return sum; // sum is 10 (1+2+3+4)

Next Steps

Continue to Language Basics to learn about variables, conditionals, loops, and more.