TypeScript Variable Declaration
In TypeScript, we use the let and const keywords to declare variables. This is consistent with modern JavaScript (ES6) practices. The var keyword is still available but generally not recommended because it has some scope issues.
1. Declaring Variables with let
let is used to declare a block-scoped local variable. This means the variable is only valid within the code block where it's declared (for example, an if statement, for loop, or function body).
let count: number = 10;
if (count > 5) {
let message: string = "Count is greater than 5"; // message is only valid within the if block
console.log(message);
}
// console.log(message); // Error: Cannot find name 'message'.Variables declared with let can have their values modified.
let age: number = 25;
age = 26; // OK2. Declaring Constants with const
const is used to declare a block-scoped constant. Constants must be initialized when declared, and their value cannot be changed once assigned.
const pi: number = 3.14159;
// pi = 3.14; // Error: Cannot assign to 'pi' because it is a constant.const declaration means the reference to the value is immutable. If the constant is an object or array, you cannot reassign it, but you can modify properties or elements inside the object or array.
const person = {
name: "Alice",
age: 30
};
// Modifying object properties is allowed
person.name = "Bob"; // OK
// But reassigning the entire object is not allowed
// person = { name: "Charlie", age: 40 }; // Error
const numbers: number[] = [1, 2, 3];
// Modifying array elements is allowed
numbers.push(4); // OK
// But reassigning the entire array is not allowed
// numbers = [5, 6, 7]; // Error3. Type Annotation
TypeScript allows you to explicitly specify a variable's type when declaring it, which is called "type annotation." The syntax is to add a colon (:) and the type name after the variable name.
let myName: string = "John Doe";
let myAge: number = 42;
let isStudent: boolean = false;Type annotations are optional. If you initialize a variable when declaring it, TypeScript will use type inference to automatically determine the variable's type.
let city = "New York"; // TypeScript infers city's type as string
let year = 2024; // TypeScript infers year's type as numberAlthough type inference is convenient, explicitly adding type annotations is good practice in the following situations:
- When a variable is not immediately initialized.
- When a function's return value type is not obvious.
- When you want the code's intent to be clearer and more explicit.
Variable Scope
Scope determines which areas of code can access a variable.
- Block Scope: Variables declared with
letandconsthave block scope. They are only valid within the nearest containing code block ({...}). - Function Scope: Variables declared with
varhave function scope. They are visible throughout the entire function, regardless of which code block they're declared in. - Global Scope: Variables declared outside all functions and code blocks have global scope and can be accessed anywhere in the code.
let globalVar = "I am global";
function myFunction() {
let functionVar = "I am in a function"; // Function scope
if (true) {
let blockVar = "I am in a block"; // Block scope
console.log(blockVar);
console.log(functionVar);
console.log(globalVar);
}
// console.log(blockVar); // Error: blockVar is not defined here
}
myFunction();