V

Variables

TypeScript supports let and const for variable declarations with type inference and explicit type annotations.

Code

typescript
1// Type inference - TypeScript infers the type
2let message = "Hello, TypeScript!";
3let count = 42;
4let isActive = true;
5
6// Explicit type annotations
7let username: string = "alice";
8let age: number = 25;
9let isLoggedIn: boolean = false;
10
11// const for immutable bindings
12const API_URL = "https://api.example.com";
13const MAX_RETRIES = 3;
14
15// Arrays with type annotations
16let numbers: number[] = [1, 2, 3, 4, 5];
17let names: string[] = ["Alice", "Bob", "Charlie"];
18
19// Alternative array syntax
20let scores: Array<number> = [95, 87, 92];
21
22console.log(message);
23console.log(`User: ${username}, Age: ${age}`);
24console.log(`Numbers: ${numbers.join(", ")}`);

Run this example locally

$ npx ts-node basics/variables.ts