Last Updated : 09 Jun, 2025
In TypeScript, variables are used to store values that can be referenced and manipulated throughout your code. TypeScript offers three main ways to declare variables: let, const, and var. Each has different behavior when it comes to reassigning values and scoping, allowing us to write more reliable and understandable code.
Types of Variable DeclarationsIn TypeScript, we can declare variables in several ways.
1. Declare Type and Value in a Single Statement typescript
let name: string = 'Amit';
const age: number = 25;
let city: string;
console.log(city);
let country = 'India';
console.log(country);
TypeScript allows you to declare variables using three keywords: var, let, and const. Here's a breakdown of how each works:
1. varvar is function-scoped and can lead to unexpected behavior due to hoisting. It’s accessible throughout the function in which it’s declared but has function-level scoping.
JavaScript
function testVar() {
var globalVar = "I am a function-scoped variable";
console.log(globalVar);
}
testVar();
Output
I am a function-scoped variable
In this example
2. letNote => Avoid using var in modern TypeScript due to its unpredictable behavior.
let provides block-level scoping, meaning it is confined to the block (i.e., loop or condition) in which it is declared. It helps prevent redeclaration within the same scope and reduces issues related to hoisting.
JavaScript
let count = 5;
if (count > 0) {
let message = "Count is positive";
console.log(message);
}
// console.log(message); // Error: message is not accessible here
Output
Count is positive
In this example
Similar to let in terms of scoping, const is used for variables that should not be reassigned after their initial value. Attempting to reassign a const variable results in a compile-time error.
JavaScript
const country = "India";
// country = "USA"; // Error: Cannot assign to 'country' because it is a constant
console.log(country);
Output
India
In this example
Type Annotations in TypeScript
- Variable names can contains alphabets both Upper-case as well as Lower-case and digits also.
- Variable names can’t start with a digit.
- We can use _ and $ special characters only, apart from these other special characters are not allowed.
Type annotations allow you to explicitly define the type of a variable, improving code clarity and reducing the risk of errors. Using explicit types helps TypeScript catch errors during development and ensures better maintainability.
Now let's understand this with the help of example:
JavaScript
let userName: string = "Arjun";
let age: number = 25;
let isActive: boolean = true;
function greetUser(name: string, age: number): string {
return `Hello, ${name}! You are ${age} years old.`;
}
let greeting = greetUser(userName, age);
console.log(greeting);
Output
Hello, Arjun! You are 25 years old.
In this example
Understanding variable scope is crucial for managing the accessibility and lifespan of variables in TypeScript. There are three main types of scopes:
1. Local ScopeVariables declared within a function or block are accessible only within that function or block.
JavaScript
function testLocalScope() {
let localVar = "I am local";
console.log(localVar);
}
// console.log(localVar); // Error: localVar is not defined outside the function
Output
I am local
In this example
Variables declared outside any function or block are accessible throughout the entire program.
JavaScript
let globalVar = 10;
function displayGlobalVar() {
console.log(globalVar);
}
displayGlobalVar();
Output
10
In this example
Variables declared within a class are accessible to all members (methods) of that class.
JavaScript
class Employee {
salary: number = 50000;
printSalary(): void {
console.log(`Salary: ${this.salary}`);
}
}
const emp = new Employee();
emp.printSalary();
Output
50000
In this example
Now let's understand variables with this example:
JavaScript
let globalVar: number = 10;
class Geeks {
private classVar: number = 11;
assignNum(): void {
let localVar: number = 12;
console.log('Local Variable: ' + localVar);
}
}
console.log('Global Variable: ' + globalVar);
let obj = new Geeks();
obj.assignNum();
Output:
Global Variable: 10Conclusion
Local Variable: 12
In TypeScript, variables are essential for storing and manipulating data. By using let, const, and var, TypeScript allows us to define variables with different scoping and reassignment rules, enhancing code reliability. Understanding type annotations helps catch errors early, improving code clarity
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4