Variables & Constants
Declaring Variables: let, const, and var
TypeScript provides three ways to declare variables: let, const, and var. Each has different behavior regarding reassignment and scope.
- `let` allows reassignment and has block scope.
- `const` prevents reassignment and has block scope.
- `var` has function scope and allows redeclaration, which can lead to unexpected behavior.
// let allows reassignment
let count = 10;
count = 20; // Allowed
// const prevents reassignment
const pi = 3.14;
pi = 3.1415; // Error: Cannot assign to 'pi' because it is a constant
// var allows redeclaration and has function scope
var name = "Alice";
var name = "Bob"; // Allowed but not recommended