TypeScript Variables
Learn how to declare variables, store values, update data, and use let, const, and var in TypeScript with practical examples and expected outputs.
What Will We Learn?
Variables are one of the fundamental building blocks of TypeScript programming.
- Understand variable declaration and initialization.
-
Declare variables using
let,const, andvar. -
Specify types such as
stringandnumber. -
Update variable values and display them using
console.log(). - Understand variable scope and common mistakes.
Variable Workflow
Follow these steps to understand how a variable is declared and used in a program.
Step 1
DeclareCreate a variable.
Step 2
InitializeAssign an initial value.
Step 3
UpdateChange the value if allowed.
Step 4
DisplayShow the variable value.
1. Variable Declaration
Variable declaration means creating a variable by giving
it a name. TypeScript supports variable declarations using
let, const, and var.
Syntax
let variableName: dataType = value;
Example
let studentName: string = "Rahul";
let age: number = 22;
console.log(studentName);
console.log(age);
Expected Output
Rahul
22
Here, studentName stores text and
age stores a numeric value.
2. Using let
The let keyword declares a block-scoped
variable whose value can be reassigned.
Example
let marks: number = 70;
console.log("Initial Marks: " + marks);
marks = 85;
console.log("Updated Marks: " + marks);
Expected Output
Initial Marks: 70
Updated Marks: 85
The value of marks changes from
70 to 85. Because its type is
number, TypeScript checks that the new value
is compatible with that type.
3. Using const
The const keyword declares a block-scoped
variable that must be initialized when declared and
cannot be reassigned later.
Example
const instituteName: string = "CIIT Training Institute";
const courseFees: number = 35000;
console.log(instituteName);
console.log(courseFees);
Expected Output
CIIT Training Institute
35000
const variable.
Use let when the variable itself needs
to be assigned a different value.
4. Using var
The var keyword is used to declare variables.
Unlike let and const,
it is function-scoped rather than block-scoped.
Example
var employeeName: string = "Amit";
console.log("Employee Name: " + employeeName);
employeeName = "Priya";
console.log("Updated Name: " + employeeName);
Expected Output
Employee Name: Amit
Updated Name: Priya
let and const for
modern TypeScript code because their block-scoping
behavior is more predictable.
5. Difference Between let, const, and var
| Feature | var | let | const |
|---|---|---|---|
| Scope | Function-scoped | Block-scoped | Block-scoped |
| Reassignment | Allowed | Allowed | Not allowed |
| Redeclaration in the same scope | Allowed | Not allowed | Not allowed |
| Must initialize during declaration | No | No | Yes |
| Recommended for new code | Usually avoid | When reassignment is needed | When reassignment is not needed |
6. Variables with Explicit Types
You can specify a type explicitly using a type annotation. This helps TypeScript detect incompatible assignments.
Example
let studentName: string = "Sneha";
let studentAge: number = 21;
let isEnrolled: boolean = true;
console.log(studentName);
console.log(studentAge);
console.log(isEnrolled);
Expected Output
Sneha
21
true
Important Type Rules
let age: number = 21;
// Compatible assignment
age = 22;
// Incompatible assignment:
// age = "Twenty Two";
The last assignment is commented out because a string cannot be assigned to a variable declared as a number. Uncommenting it will produce a TypeScript type error.
7. Variable Type Inference
TypeScript can infer a variable's type from its initial value. You do not always need to provide an explicit type annotation.
Example
let courseName = "TypeScript";
let fees = 35000;
let isOnline = true;
console.log(courseName);
console.log(fees);
console.log(isOnline);
Expected Output
TypeScript
35000
true
TypeScript infers string, number,
and boolean from the initial values.
8. Variable Scope
Scope determines where a variable can be accessed.
Variables declared with let and
const are block-scoped, while
var is function-scoped.
Example: Block Scope
if (true) {
let message: string = "Hello TypeScript";
console.log(message);
}
Expected Output
Hello TypeScript
The variable message can be accessed inside
the block where it is declared. It cannot be accessed
outside that block.
Practical Example: Student Registration
Let's use TypeScript variables to store student registration information at CIIT Training Institute.
Example
const instituteName: string = "CIIT Training Institute";
let studentName: string = "Amit";
let courseName: string = "Dot Net Full Stack";
let courseFees: number = 35000;
let isRegistered: boolean = true;
console.log("Institute: " + instituteName);
console.log("Student: " + studentName);
console.log("Course: " + courseName);
console.log("Fees: " + courseFees);
console.log("Registered: " + isRegistered);
Expected Output
Institute: CIIT Training Institute
Student: Amit
Course: Dot Net Full Stack
Fees: 35000
Registered: true
The example uses const for the institute name
and let for student details that may need
reassignment.
Common Mistakes
- Assigning a string to a variable declared as a number.
-
Trying to reassign a variable declared using
const. - Accessing a block-scoped variable outside its block.
-
Declaring a
constvariable without initializing it. -
Using
varwithout understanding its function-scoping behavior.
const by default. Use let
when reassignment is required.
Practice Task
Create a TypeScript program to store and display employee details.
- Employee Name: Priya
- Employee ID: 101
- Salary: 45000
- Is Active: true
Use suitable TypeScript data types for all four variables.
Use const for values that should not be reassigned
and let for values that may change.
Hint
const employeeName: string = "Priya";
const employeeId: number = 101;
let salary: number = 45000;
let isActive: boolean = true;
console.log(employeeName);
console.log(employeeId);
console.log(salary);
console.log(isActive);
Summary
In this lesson, we learned how to declare and initialize
variables, use let, const, and
var, specify types, understand type inference,
and work with variable scope.
Key Point:
Use const when reassignment is unnecessary,
and use let when the value needs to change.