# JSVariable

## Overview

`JSVariable` represents script variables in the Solution Model. It allows defining variables with specific types, default values, and scope, offering flexibility for use in forms, global contexts, and dynamic scenarios. Variables can be configured with types like `TEXT`, `INTEGER`, `NUMBER`, `DATETIME`, or `MEDIA`. ## Functionality

JSVariable provides constants for specifying the variable type. Properties like `defaultValue`, `name`, and `variableType` allow precise control over the variable's configuration. The `defaultValue` property can accept expressions such as dates, numbers, or text, enabling dynamic initialization. Methods like `getScopeName` and `getUUID` facilitate metadata access, such as identifying the variable's scope or its unique identifier.

Examples demonstrate how to create variables dynamically, assign default values, and use them in various scopes. Variables can also be modified to change their type or default behavior.

Variables can alternatively be created and managed using the [Variable Editor](https://docs.servoy.com/reference/servoy-developer/object-editors/variable-editor).

## Constants Summarized

| Type                                                                         | Name                  | Summary                                                                |
| ---------------------------------------------------------------------------- | --------------------- | ---------------------------------------------------------------------- |
| [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) | [DATETIME](#datetime) | Constant to be used when the type of a variable needs to be specified. |
| [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) | [INTEGER](#integer)   | Constant to be used when the type of a variable needs to be specified. |
| [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) | [MEDIA](#media)       | Constant to be used when the type of a variable needs to be specified. |
| [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) | [NUMBER](#number)     | Constant to be used when the type of a variable needs to be specified. |
| [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) | [TEXT](#text)         | Constant to be used when the type of a variable needs to be specified. |

## Properties Summarized

| Type                                                                         | Name                          | Summary                            |
| ---------------------------------------------------------------------------- | ----------------------------- | ---------------------------------- |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) | [defaultValue](#defaultvalue) | The default value of the variable. |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) | [name](#name)                 | The name of the variable.          |
| [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) | [variableType](#variabletype) | The type of the variable.          |

## Methods Summarized

| Type                                                                          | Name                            | Summary                          |
| ----------------------------------------------------------------------------- | ------------------------------- | -------------------------------- |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string)  | [getScopeName()](#getscopename) | Get scope name                   |
| [UUID](https://docs.servoy.com/reference/servoycore/dev-api/application/uuid) | [getUUID()](#getuuid)           | Returns the UUID of the variable |

## Constants Detailed

### DATETIME

Constant to be used when the type of a variable needs to be specified.

**Type**\
[Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)

**Sample**

```js
var dateVar = solutionModel.newGlobalVariable('globals', 'gDate', JSVariable.DATETIME);
dateVar.defaultValue = 'now';
application.output(scopes.globals.gDate); // Prints the current date and time.
```

### INTEGER

Constant to be used when the type of a variable needs to be specified.

**Type**\
[Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)

**Sample**

```js
var intVar = solutionModel.newGlobalVariable('globals', 'gInt', JSVariable.INTEGER);
intVar.defaultValue = 997;
application.output(scopes.globals.gInt); // Prints 997
```

### MEDIA

Constant to be used when the type of a variable needs to be specified.

**Type**\
[Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)

**Sample**

```js
var mediaVar = solutionModel.newGlobalVariable('globals', 'gMedia', JSVariable.MEDIA);
mediaVar.defaultValue = 'new Array(1, 2, 3, 4)';
application.output(scopes.globals.gMedia); // Prints out the array with four elements.
```

### NUMBER

Constant to be used when the type of a variable needs to be specified.

**Type**\
[Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)

**Sample**

```js
var numberVar = solutionModel.newGlobalVariable('globals', 'gNumber', JSVariable.NUMBER);
numberVar.defaultValue = 192.334;
application.output(scopes.globals.gNumber); // Prints 192.334
```

### TEXT

Constant to be used when the type of a variable needs to be specified.

**Type**\
[Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)

**Sample**

```js
var txtVar = solutionModel.newGlobalVariable('globals', 'gText', JSVariable.TEXT);
txtVar.defaultValue = '"some text"'; // Use two pairs of quotes if you want to assign a String as default value.
application.output(scopes.globals.gText); // Prints 'some text' (without quotes).
```

## Properties Detailed

### defaultValue

The default value of the variable.

It is interpreted as a JS expression.

For form variables, setting this property via solutionModel requires the form instances to be destroyed (history.remove("formName")).\
If you want to use a default value for a newly created variable create the variable using the 3 parameter version newVariable(name,type,defaultValue).

For INTEGER variables it can be an integer constant, like 10 for example.\
For NUMBER variables it can be a real constant, like 22.41. For DATETIME variables it can be "now", or a JS expression like "new Date()". For TEXT variables it can be any string surrounded with quotes, like "'some text'".

**Type**\
[String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) The default value of the variable.

**Sample**

```js
var intVar = solutionModel.newGlobalVariable('globals', 'gInt', JSVariable.INTEGER);
intVar.defaultValue = 997;
application.output(scopes.globals.gInt); // Prints 997
var numberVar = solutionModel.newGlobalVariable('globals', 'gNumber', JSVariable.NUMBER);
numberVar.defaultValue = 192.334;
application.output(scopes.globals.gNumber); // Prints 192.334
var dateVar = solutionModel.newGlobalVariable('globals', 'gDate', JSVariable.DATETIME);
dateVar.defaultValue = 'now';
application.output(scopes.globals.gDate); // Prints the current date and time.
var txtVar = solutionModel.newGlobalVariable('globals', 'gText', JSVariable.TEXT);
txtVar.defaultValue = '"some text"'; // Use two pairs of quotes if you want to assign a String as default value.
application.output(scopes.globals.gText); // Prints 'some text' (without quotes).
var mediaVar = solutionModel.newGlobalVariable('globals', 'gMedia', JSVariable.MEDIA);
mediaVar.defaultValue = 'new Array(1, 2, 3, 4)';
application.output(scopes.globals.gMedia); // Prints out the array with four elements.
```

### name

The name of the variable.

**Type**\
[String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) The name of the variable.

**Sample**

```js
var gVar = solutionModel.newGlobalVariable('globals', 'gtext', JSVariable.TEXT);
gVar.name = 'anotherName';
gVar.defaultValue = '"default text"';
// The next two lines will print the same output.
application.output(scopes.globals[gVar.name]);
application.output(scopes.globals.anotherName);
```

### variableType

The type of the variable. Can be one of: TEXT, INTEGER, NUMBER, DATETIME or MEDIA.

**Type**\
[Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) The type of the variable (e.g., TEXT, NUMBER, DATETIME).

**Sample**

```js
var g = solutionModel.newGlobalVariable('globals', 'gtext',JSVariable.TEXT);
scopes.globals.gtext = 'some text';
g.variableType = JSVariable.DATETIME;
scopes.globals.gtext = 'another text'; // This will raise an error now, because the variable is not longer of type text.
```

## Methods Detailed

### getScopeName()

Get scope name

**Returns:** [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) The scope name in which the variable is defined.

**Sample**

```js
var globalVariables = solutionModel.getGlobalVariables();
for (var i in globalVariables)
	application.output(globalVariables[i].name + ' is defined in scope ' + globalVariables[i].getScopeName());
```

### getUUID()

Returns the UUID of the variable

**Returns:** [UUID](https://docs.servoy.com/reference/servoycore/dev-api/application/uuid) The unique identifier (UUID) of the variable.

**Sample**

```js
var dateVar = solutionModel.newGlobalVariable('globals', 'gDate', JSVariable.DATETIME);
application.output(dateVar.getUUID().toString());
```

***
