Click here to Skip to main content
14,086,361 members
Click here to Skip to main content
Add your own
alternative version

Stats

2.7K views
2 bookmarked
Posted 21 Jun 2018
Licenced CPOL

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

, 21 Jun 2018
Rate this:
Please Sign up or sign in to vote.
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kunal Chowdhury «IN»
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190518.1 | Last Updated 21 Jun 2018
Article Copyright 2018 by Kunal Chowdhury «IN»
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid