Variables
Variables in Risor are declared with the let keyword and can be reassigned
after declaration. Use const for values that should not change.
Declarations
Use let to declare a new variable and assign it a value:
let name = "Alice"
let x = 42
let active = trueEvery variable must be initialized at the point of declaration. There is no uninitialized variable state in Risor.
Constants
Use const to declare a value that cannot be reassigned:
const MAX_RETRIES = 3
const API_VERSION = "v2"Attempting to reassign a const will produce a compile-time error.
Reassignment
After a variable has been declared, you can reassign it using =:
let x = 42
x = 100Reassignment does not require the let keyword. Using let again with the
same name in the same scope will shadow the previous variable.
Compound Assignment
Risor supports compound assignment operators that combine an arithmetic operation with assignment:
let x = 10
x += 5 // x is now 15
x -= 3 // x is now 12
x *= 2 // x is now 24
x /= 4 // x is now 6The supported compound assignment operators are:
| Operator | Description | Equivalent |
|---|---|---|
+= | Add and assign | x = x + y |
-= | Subtract and assign | x = x - y |
*= | Multiply and assign | x = x * y |
/= | Divide and assign | x = x / y |
Scope
Variables are scoped to the block in which they are declared. A variable declared inside a function or block is not visible outside of it:
let x = 1
function example() {
let y = 2
x = 10 // can access outer variable
}
// y is not accessible hereInner scopes can read and modify variables from outer scopes, which is the foundation for closures in Risor.