Loading

TypeScript

Variables in TypeScript. The Complete TypeScript Developer Course 2023 [Videos].

Variable: Variable is a named place in memory where some data/value can be stored. According to the word variable, it can be said that the value of a variable can be changed/vary. While declaring a variable, some rules have to be followed:

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

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.

    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.

    Code #1:

    var global_var = 10         //global variable 
    class Geeks { 
       geeks_var = 11;             //class variable 
       assignNum():void { 
          var local_var = 12;    //local variable 
       
    document.write("Global Variable: "+global_var)  
    var obj = new Geeks(); 
    document.write("Class Variable: "+obj.geeks_var) 

    Output:

    Global Variable: 10
    Class Variable: 11
  • See All

    Comments (172 Comments)

    Submit Your Comment

    See All Posts

    Related Posts

    TypeScript / Blog

    Difference between TypeScript and JavaScript

    When JavaScript was developed the JavaScript development team introduced JavaScript as a client-side programming language. But when people were using JavaScript then developers get to know that JavaScript can be used as a server-side programming language also. But When JavaScript was growing then the code of JavaScript became complex and heavy.
    19-jan-2022 /33 /172

    TypeScript / Blog

    What is the difference between interface and type in TypeScript ?

    Both the methods Type and the Interface are used to describe the structure of the objects in TypeScript. But holds some specific features that will be helpful according to the situation, choosing between them and totally depends on the developer.
    /33 /172

    TypeScript / Blog

    What is the Function type in TypeScript ?

    TypeScript is a JavaScript-based programming language with a typed syntax. It provides improved tools of any size. It adds extra syntax to JavaScript. This helps in facilitating a stronger interaction between you and your editor. It also helps in catching the mistakes well in advance.
    19-jan-2022 /33 /172