← Back to the index

5. Variables and Declarations

In this chapter

Variable Declaration (let)

Variables are declared using the let keyword followed by an identifier and an initial value.

let x = 10
let message = "Hello"

Variable Assignment (=)

Existing variables can be re-assigned new values using the = operator.

let x = 10
x = 20 // Reassignment

Variable Scope

Variables in vv are lexically scoped. The scope of a variable is the region of code where it is visible and can be accessed.

Block Scope

Variables declared within a block (such as an if statement, while loop, or begin...end block) are only accessible within that block and any nested blocks.

let x = 1
if true
    let y = 2
    console.print(x + y) // OK: x and y are in scope
end
// console.print(y) // Error: y is out of scope

Shadowing

vv allows shadowing, which means you can declare a new variable in an inner scope with the same name as a variable in an outer scope. The inner variable "shadows" the outer one until the inner scope ends.

let x = 1
begin
    let x = 2
    console.print(x) // Prints 2
end
console.print(x) // Prints 1