Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What are Variables in TypeScript?

Variables are an essential part of any programming language. They are the containers that store the data your program uses and manipulates. In this article, we will discuss what variables are in the context of TypeScript, a popular statically-typed superset of JavaScript. We will cover the basics of declaring and using variables, as well as some advanced topics such as variable types and scope.

Variables: the fundamentals

To understand what variables are, let's use an analogy. Imagine that you have a row of empty boxes, and you want to store various items in them, such as books, pens, and papers. You could label each box with a name, like "box_of_books" or "box_of_pens," to help you remember what is inside. In programming, these labels are called "variables," and the items inside the boxes represent the data stored in the variables.

In TypeScript, we declare a variable using the let or const keyword, followed by the variable name and an optional type annotation. The type annotation specifies the type of data that the variable can store. Here's an example:

let box_of_books: number = 5;
let box_of_pens: string = "blue";

In this example, we declare two variables: box_of_books and box_of_pens. The box_of_books variable can store a number, while the box_of_pens variable can store a string (a sequence of characters).

Declaring variables with let

The let keyword is used to declare a variable that can be reassigned a new value later on. Here's a simple example:

let box_of_books: number = 5;
console.log(box_of_books); // Output: 5

box_of_books = 10;
console.log(box_of_books); // Output: 10

In this example, we first declare the box_of_books variable with an initial value of 5. We then reassign its value to 10, and the console output reflects this change.

Declaring variables with const

The const keyword is used to declare a variable whose value cannot be changed after it has been assigned. In other words, it creates a read-only reference to a value. Here's an example:

const box_of_papers: number = 20;
console.log(box_of_papers); // Output: 20

box_of_papers = 30; // Error: Cannot assign to 'box_of_papers' because it is a constant.

In this example, we declare the box_of_papers variable with an initial value of 20. When we try to reassign its value to 30, TypeScript gives an error, indicating that the value of a const variable cannot be changed.

Variable types

In TypeScript, each variable has a specific type, which determines the kind of data it can store. TypeScript has several built-in types that you can use when declaring variables:

  • number: for storing numeric values, both integers and floating-point numbers.
  • string: for storing sequences of characters, such as text.
  • boolean: for storing true or false values.
  • any: for storing values of any type, without any type checking. This type is generally discouraged, as it defeats the purpose of using TypeScript's type system.

Here's an example that demonstrates the use of these types:

let numberOfBooks: number = 10;
let bookTitle: string = "To Kill a Mockingbird";
let isAvailable: boolean = true;
let unknownType: any = "This can be anything!";

In addition to the built-in types, TypeScript allows you to define custom types using interfaces, classes, and other constructs. We won't delve into these advanced topics in this article, but they are essential when building complex applications.

Type inference

One of the advantages of TypeScript over JavaScript is its type inference capabilities. This means that TypeScript can often determine the type of a variable without you explicitly specifying it. For example:

let numberOfBooks = 10; // TypeScript infers the type as 'number'
let bookTitle = "To Kill a Mockingbird"; // TypeScript infers the type as 'string'

In this example, even though we didn't specify the types of numberOfBooks and bookTitle, TypeScript correctly infers them as number and string, respectively.

However, there are cases when TypeScript cannot infer the type, and you must provide an explicit type annotation. Here's an example:

let unknownType; // TypeScript infers the type as 'any'

unknownType = 10;
unknownType = "Hello, world!";

In this example, we declare the unknownType variable without an initial value or a type annotation. TypeScript infers its type as any, which means it can store values of any type.

Variable scope

The scope of a variable determines where it can be accessed and modified in your code. In TypeScript, there are two main types of scope: global and local.

Global scope

A variable declared outside of any function or block of code has a global scope. This means it can be accessed from anywhere in your code. Here's an example:

let globalVar: number = 10;

function printGlobalVar() {
  console.log(globalVar); // Output: 10


In this example, we declare the globalVar variable in the global scope, which allows it to be accessed from within the printGlobalVar function.

Local scope

A variable declared inside a function or block of code has a local scope. This means it can only be accessed from within that function or block of code. Here's an example:

function printLocalVar() {
  let localVar: number = 20;
  console.log(localVar); // Output: 20

console.log(localVar); // Error: Cannot find name 'localVar'

In this example, we declare the localVar variable inside the printLocalVar function. It can be accessed and modified within that function but not outside of it. When we try to access it from outside of the function, TypeScript gives an error, indicating that the variable is not defined in this scope.


In this article, we explored the basics of variables in TypeScript, including declaring and using variables, variable types, type inference, and variable scope. Understanding these concepts is crucial for anyone learning to program in TypeScript, as they form the foundation for more advanced topics.

As you continue to learn and work with TypeScript, you will likely encounter more advanced concepts such as interfaces, classes, and generics. These constructs will further expand your understanding of variables and their role in building complex applications. But for now, you should have a solid grasp on the fundamentals of variables in TypeScript, and you're well-equipped to start writing your own TypeScript programs.