# Variables
Variables are storage locations with a specific type. DWScript supports both modern inline declarations and traditional Pascal-style blocks.
## Explicit Declaration
You can specify the type explicitly using the `name : Type` syntax.
```pascal
var x : Integer;
var name : String;
x := 10;
name := 'Alice';
// OUTPUT NONE
```
### Typed Initialization
You can combine declaration and assignment in a single statement.
```pascal
var f : Float := 10;
var status : String := 'Initializing';
PrintLn(f);
// OUTPUT
// 10
```
## Type Inference
Modern DWScript can infer the type of a variable from its initial assignment. This is the preferred style for local variables as it reduces redundancy.
```pascal
var count := 42; // Inferred as Integer
var price := 19.99; // Inferred as Float
var message := 'Hello'; // Inferred as String
var active := True; // Inferred as Boolean
// OUTPUT NONE
```
## Declaration Styles
### Modern (Inline)
In script mode, variables can be declared anywhere before their first use.
```pascal
PrintLn('Step 1');
var x := 100;
PrintLn(x);
// OUTPUT
// Step 1
// 100
```
### Classic Pascal Style
DWScript also supports the classic Pascal structure where variables are declared in a `var` block before the `begin` keyword of a procedure or function.
```pascal
procedure ProcessData;
var
i : Integer;
s : String;
begin
i := 5;
s := 'Value: ';
PrintLn(s + IntToStr(i));
end;
ProcessData;
// OUTPUT
// Value: 5
```
## Reserved Names & Escaping
If you need to use a reserved Pascal keyword (like `type`, `begin`, `end`, `repeat`, etc.) as an identifier for a variable, field, or function, you can prefix it with an ampersand `&`.
The compiler will treat the identifier as if the ampersand were not there, allowing you to interface with external systems (like JSON APIs) that use these names.
```pascal
var &type := 'System';
var &repeat := True;
PrintLn(&type);
// OUTPUT
// System
```
## Scope & Lifetime
Variable visibility and lifetime are determined by where they are declared:
* **Global Scope:** Variables declared at the top level of a script are visible everywhere and persist for the duration of the script execution.
* **Local Scope:** Variables declared inside a `procedure`, `function`, or `block` (like a `for` loop) are only visible within that block. Their lifetime is tied to the procedure they are in.
* **Shadowing:** A local variable can have the same name as a global one; in this case, the local variable "shadows" the global one within its scope.
```pascal
var x := 'Global';
procedure Test;
var x := 'Local'; // Shadows global x
begin
PrintLn(x);
end;
Test;
PrintLn(x);
// OUTPUT
// Local
// Global
```
:::info
Variables in DWScript are **guaranteed** to be initialized to their default values (0 for numbers, '' for strings, nil for objects/arrays, False for booleans). You do not need to manually initialize them to zero/nil.
:::
## See Also
* **[Data Types](/lang/basics/types)** - Overview of the type system.
* **[Constants](/lang/basics/constants)** - Values that do not change.
* **[Operators](/lang/basics/operators)** - Using variables in expressions.
* **[State Management](/lang/basics/state)** - Persistent variables in web environments.
Variables
Variables are storage locations with a specific type. DWScript supports both modern inline declarations and traditional Pascal-style blocks.
Explicit Declaration
You can specify the type explicitly using the name : Type syntax.
var x :Integer;var name :String;
x :=10;
name :='Alice';
Typed Initialization
You can combine declaration and assignment in a single statement.
var f :Float:=10;var status :String:='Initializing';
PrintLn(f);
Result
10
Type Inference
Modern DWScript can infer the type of a variable from its initial assignment. This is the preferred style for local variables as it reduces redundancy.
var count :=42;// Inferred as Integervar price :=19.99;// Inferred as Floatvar message :='Hello';// Inferred as Stringvar active :=True;// Inferred as Boolean
Declaration Styles
Modern (Inline)
In script mode, variables can be declared anywhere before their first use.
PrintLn('Step 1');var x :=100;
PrintLn(x);
Result
Step 1
100
Classic Pascal Style
DWScript also supports the classic Pascal structure where variables are declared in a var block before the begin keyword of a procedure or function.
procedure ProcessData;var
i :Integer;
s :String;begin
i :=5;
s :='Value: ';
PrintLn(s + IntToStr(i));end;
ProcessData;
Result
Value: 5
Reserved Names & Escaping
If you need to use a reserved Pascal keyword (like type, begin, end, repeat, etc.) as an identifier for a variable, field, or function, you can prefix it with an ampersand &.
The compiler will treat the identifier as if the ampersand were not there, allowing you to interface with external systems (like JSON APIs) that use these names.
var &type:='System';var &repeat:=True;
PrintLn(&type);
Result
System
Scope & Lifetime
Variable visibility and lifetime are determined by where they are declared:
Global Scope: Variables declared at the top level of a script are visible everywhere and persist for the duration of the script execution.
Local Scope: Variables declared inside a procedure, function, or block (like a for loop) are only visible within that block. Their lifetime is tied to the procedure they are in.
Shadowing: A local variable can have the same name as a global one; in this case, the local variable "shadows" the global one within its scope.
var x :='Global';procedure Test;var x :='Local';// Shadows global xbegin
PrintLn(x);end;
Test;
PrintLn(x);
Result
Local
Global
Variables in DWScript are guaranteed to be initialized to their default values (0 for numbers, '' for strings, nil for objects/arrays, False for booleans). You do not need to manually initialize them to zero/nil.