65.9K
CodeProject is changing. Read more.
Home

How to Declare Variables in TypeScript? -- TypeScript Tutorial for Beginners

Jun 21, 2018

CPOL

2 min read

viewsIcon

7448

In this chapter, we will learn various ways to declare variables in TypeScript.

In the previous few chapters of the TypeScript Tutorial series, we learned how to install TypeScript and then begin with creating a simple HelloWorld application. We also learned about TypeScript configuration file (tsconfig.json).

In this chapter, we will learn various ways to declare variables in TypeScript. Continue reading to learn more about it.

Declaring Variables with "var" Keyword

Just like JavaScript, you can also define variables in TypeScript using the keyword var. For example, var message = "Hello World!";. Defining variables using the var keyword has some problems and most developers face this issue. Let's take a few examples to understand it in a better way.

You can define a variable inside a block and use it outside the scope, like this:

function GetValue(initialize: boolean) {
    if (initialize) {
        var value = 100;
    }

    return value;
}

GetValue(true);  // returns "100"
GetValue(false); // returns "undefined"

Using the var keyword, you can declare the same variable multiple times within the same code block. For example, the following code will execute without any error:

function GetValue(value, condition) {
    var value = 10;
    ...
    ...
    var value = 25; // no error
    ...
    ...
    if (condition) {
        var value = 100; // no error
        ...
    }
}

Declaring Variables with "let" Keyword

To overcome the problems that arise with variable declaration using the var keyword, TypeScript introduces variable declaration using let keyword. You can write the let statements in the same way that you write the var statements. For example, let message = "Hello World!";.

Unlike var, the let statement uses block-scoping. That means, if you declare a variable with let keyword in a function or a block, the scope of the variable will be limited to the same function or code block and won't be accessible outside of their nearest containing block.

function GetValue(initialize: boolean) {
    if (initialize) {
        var value = 100;
    }

    return value; // error
}

It's also a point to note that you can't redeclare a variable using let keyword within the same scope, which was a problem with var declaration. The following code will throw an error during compile time:

function GetValue(condition) {
    let value = 10;
    ...
    ...
    let value = 25; // ERROR: re-declaration of "value"
    ...
    ...
    if (condition) {
        let value = 100;
        ...
    }
}

Declaring Variables with "const" Keyword

You can also declare variables using the const keyword. For example, const message = "Hello World!";. The const keyword acts like let but with a difference that their values cannot be changed after they are initialized and hence you cannot re-assign values to them.

👉 TypeScript Tutorial - Getting started with TypeScript