Variables
A variable is a container that holds a value. You must give it a name, and then you can use this name to refer to the value stored inside it.
OneVision Scripts builds on JavaScript by adding a feature called static typing which makes it easier to write safe and error-free code.
Let's explore the variable's basics:
Declaring variables
In OneVision Scripts, you can declare different types of variables using different keywords:
var: Declares a variable that can be used throughout the entire script it’s declared in.global var: Addingglobalto the variable defintion makes the it usable in all scripts. This means that it can be read or modified in any script, and by any function or block.const var/const global var: Addingconstto the variable definition makes it read only. This means the variable cannot be modified anywere in the code. This is specially useful to define configuration parameters.pglobal var: Addingpglobalto the variable definition makes the variable permanent global. This means it is a global variable that will keep the value after stopping the runtime.
For example:
var a = 5;
global var b = 10;
const var c = 15;
const global var d = 20;
pglobal var e = 25;
Variable types
Variables may hold values that have different data types:
| Type | Description | Example |
|---|---|---|
number | Numeric values, including integers and floating-point numbers. | var num = 42; |
string | Textual data, sequences of characters enclosed in quotes. | var str = "Hello, world!"; |
boolean | Logical values. Possible values are true and false. | var isTrue = true; |
list | Ordered collections of values. Allows storing multiple elements in a single reference. | var list = [1, 2, 3, 4, 5]; |
object | Unordered and static collections of key-value pairs. | var obj = {item1: 23, item2: "apple", item3: true}; |
record | Unordered and dynamic collections of key-value pairs. | var rec: Record<string, number[]> = {"a": [1, 2, 3], "b": [4]}; |
any | Allows to define a variable when the type is unknown. | var mixedList: any[] = [1, "Hello", true]; |
Adding types to variables
OneVision Scripts allows you to specify what type of data a variable will hold. This is called a type annotation.
var age: number = 30; // age must be a number
var name: string = "Alice"; // name must be a string
var isStudent: boolean = true; // isStudent must be a boolean (true or false)
OneVision Scripts is smart and can guess the type of a variable based on the value you give it, so you don't have to explicitly state the type. This is called type inference.
var city = "Barcelona"; // OneVision knows 'city' is a string
var score = 100; // OneVision knows 'score' is a number
You can also declare lists with a specific type.
var scores: number[] = [90, 85, 88]; // array of numbers
var players: string[] = ["Maqsood Ahmed", "Gogi Alauddin", "Ibrahim Amin"]; // array of strings
OneVision can infer the type of lists as well.
var scores = [90, 85, 88]; // OneVision knows the variable scores will contain a list of numbers
You cannot declare a variable with an empty list.
var players = []; // Error
Define the type of the list to declare an empty list.
var players: string[] = []; // Correct
Type Assertions
Sometimes you know more about the type of a variable than OneVision Scripts does. You can use type assertions to specify the type you know it should be.
var someValue: any = "this is a string";
var strValue: string = someValue as string;