Variables

Declare mutable bindings with `var`.

Use var to create a variable. You can omit the initializer: it defaults to nil.

Assign later with =. Reassignment works for locals and instance fields.

Declare

Create a binding with `var`. Omit the initializer and it starts as `nil`.

Declare
var name = "Rain";
var count = 0;
print name;
print count;
output
Rain
0

Reassign

Update an existing variable with `=`. Works for locals and globals.

Reassign
var n = 1;
n = n + 1;
print n;
output
2

Block scope

A `var` inside `{ }` shadows the same name in an outer scope for that block.

Block scope
var x = "outer";
{
  var x = "inner";
  print x;
}
print x;
output
inner
inner

Notes