Open In App

Variables in TypeScript

Improve
Improve
Like Article
Like
Save
Share
Report

The variable is a named place in memory where some data/value can be stored. They serve as containers for storing data values, allowing to store numeric, string, boolean, and other types of information. In this TypeScript Variable Article, we’ll learn how to declare, manipulate, and utilize variables in TypeScript.

Variable Declaration in TypeScript

1. var, let, and const

In TypeScript, we have three primary keywords for variable declaration:

  • var: Traditionally used in JavaScript, it has some quirks and scoping rules that can lead to unexpected behaviour. Variables declared with var are accessible anywhere within their containing function, module, namespace, or global scope.
  • let: Introduced to address the limitations of var, let provides block-scoping. It allows you to declare variables that are confined to specific blocks of code (such as loops or conditional statements). This helps prevent accidental variable redeclaration and enhances code clarity.
  • const: Similar to let, const also has block-scoping. However, it goes a step further by preventing re-assignment after initialization. Once you assign a value to a const variable, you cannot change it. Use const for values that should remain constant throughout your program.

2. Rules have to be followed:

  • Variable names can contains alphabets both Upper-case as well as Lower-case and digits also.
  • Variable names can’t start with a digit.
  • We can use _ and $ special characters only, apart from these other special characters are not allowed.

3. Ways to Variable declaration:

We can declare a variable in multiple ways like below:

  • var Identifier:Data-type = value;
  • var Identifier: Data-type;
  • var Identifier = value;
  • var Identifier;
  • Examples:

    Variable declarationDescription
    var name:number = 10;Here name is a variable which can store only Integer type data.
    var name:number;Here name is a variable which can store only Integer type data. But by default its value set to undefined.
    var name = 10;Here while declaring variable we are not specifying data-type. Therefore compiler decide its data type by seeing its value i.e. number here.
    var name;Here while declaring variable we are not specifying data-type as well as we are not assigning any value also. Then compiler takes its data type as any. Its value is set to undefined by default.

    3.  Type Annotations

    TypeScript enhances variables with type annotations, making it a strongly typed language. When declaring a variable, you can specify its type explicitly. 

    Example:

    let name: string = 'Amit';
    let age: number = 25;
    const country: string = 'Noida';

    4. Variable scopes in TypeScript:

    Here scope means the visibility of variable. The scope defines that we are able to access the variable or not. TypeScript variables can be of the following scopes:

    • Local Scope:As the name specified, are declared within the block like methods, loops etc. Local variables are accessible only within the construct where they are declared.
    • Global Scope:If the variable is declared outside the construct then we can access the variable anywhere. This is known as Global Scope.
    • Class Scope:If a variable is declared inside the class then we can access that variable within the class only.

    Example:

    javascript
    let global_var: number = 10; // global variable
    
    class Geeks {
        geeks_var: number = 11; // class variable
        assignNum(): void {
            let local_var: number = 12; // local variable
        }
    }
    
    console.log("Global Variable: " + global_var);
    
    let obj = new Geeks();
    console.log("Class Variable: " + obj.geeks_var);
    

    Output:

    Global Variable: 10
    Class Variable: 11

    Last Updated : 15 Mar, 2024
    Like Article
    Save Article
    Previous
    Next
    Share your thoughts in the comments
    Similar Reads