Variables

Variables in Kode are dynamically typed and declared using the let keyword. Type annotation is optional — Kode infers types automatically.

Variable Declaration with Type Inference

kode
let name = "Alice"           // Inferred: string
let age = 30                 // Inferred: int
let score = 95.5             // Inferred: float
let isActive = true          // Inferred: bool
let values = [1, 2, 3]       // Inferred: [int]

Variable Declaration with Explicit Types

kode
let name: string = "Alice"
let age: int = 30
let score: float = 95.5
let isActive: bool = true
let values: [int] = [1, 2, 3]

Reassignment

Variables declared with let are mutable and can be reassigned:

kode
let x = 10
x = 20          // Valid
x = "string"    // Type changes (duck typing)

Variable Scope

Variables are block-scoped:

kode
let x = 10

if (true) {
    let x = 20  // Different variable
    print(x)    // 20
}

print(x)        // 10

Naming Conventions

  • Variable names can contain letters, digits, and underscores
  • Must start with a letter or underscore
  • Case-sensitive
  • Cannot use keywords as variable names
kode
let userName = "Alice"      // camelCase
let user_name = "Bob"       // snake_case
let _private = 42           // Starting with underscore
let value123 = 100          // With numbers