Variables

With Bosscript being a dynamic programming language, any variable can hold any type of data. There are two types of variables in Bosscript, those declared with var and those declared with konst.

Var

Variables declared with var are mutable, which means that they can be reassigned at any point in the program. They also don’t need to be explicitly initialized when declared.

var x;

The variable x from the example above is automatically initialized to nedefinisano (null). Of course, it is possible to initialize a variable with any expression:

var x = 10;
var y = example();
var z = "Z";

You can even initialize a variable to nedefinisano explicitly, if you really want to.

var x = nedefinisano;

This can be considered redundant, though.

It is also possible to declare multiple variables at once:

var x, y;

In this case, both x and y are initialized to nedefinisano.

Any combination of initialized and non-initialized variables works:

var x, y = 10;
var z = 10, s;
var a, b, c;

As previously mentioned, var variables can be reassigned at any point. Consider the example below:

var x = 10;

x = 20;

ispis(x); // prints 20

x was reassigned to the number 20.

You can also reassign values of other types, like below:

var x = 10;

x = "Hello";

ispis(x); // prints Hello

In this case, we changed x from the number 10 to the string “Hello”.

If need be, variables can be reassigned to nedefinisano again:

var x = 10;

x = nedefinisano;

ispis(x); // prints nedefinisano

The only thing that is not allowed is declaring the same variable multiple times in the same scope:

var taken = 10;
var taken = "Hello"; 

The code above will cause an error: taken has already been defined

Konst

The keyword konst stands for konstanta (constant) and, as the name suggests, is used to declare constants. Once defined, constants cannot be reassigned. They also must be initialized upon creation, unlike with var.

konst pi = 3.14; // proper
konst invalid;

Trying to reassign a konst will result in an error Constants cannot be reassigned:

konst x = 10;
x = 20;

Mutability of objects

Contrary to what you may think at first, declaring arrays and objects as konst doesn’t make them immutable. Instead, it just makes the variable un-reassignable. Consider the following example:

konst obj = {
    a: 10,
    b: 20
};

obj.a = 20;

ispis(obj); // prints {a: 20, b: 20}

The code above is completely valid. If you need your objects to be immutable, consider using models. The same is true for arrays:

konst arr = [1, 2, 3, 4];

arr[2] = 999;

ispis(arr); // prints [1, 2, 999, 4]

This is what konst disallows with objects and arrays:

konst obj = {
    a: "A",
    b: "B"
};

obj = {};

konst arr = [1, 2, 3, 4];

arr = [9, 8, 7];

So, just like with other data types, a konst holding an array or object cannot be reassigned.