Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Declare Variables in TypeScript

DZone's Guide to

How to Declare Variables in TypeScript

Though TypeScript is a superset of JavaScript, it does have some peculiarities of its own. Read on to learn more about declaring variables in TypeScript.

· Web Dev Zone ·
Free Resource

Learn how error monitoring with Sentry closes the gap between the product team and your customers. With Sentry, you can focus on what you do best: building and scaling software that makes your users’ lives better.

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

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

Declaring Variables With the "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 few examples to understand it better.

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 the let keyword. You can write the let statements 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 thelet 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 the let keyword within the same scope, which was a problem with the 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 the "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.

What’s the best way to boost the efficiency of your product team and ship with confidence? Check out this ebook to learn how Sentry's real-time error monitoring helps developers stay in their workflow to fix bugs before the user even knows there’s a problem.

Topics:
web dev ,typescript ,web application development ,typescript variables

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}