TypeScript tutorial: Lesson 2 – Variables


TypeScript – Variables

Variable rules:

  • Variable names can contain alphabets and numeric digits.
  • They cannot contain spaces and special characters, except the underscore (_) and the dollar ($) sign.
  • Variable names cannot begin with a digit.

Variable Declaration in TypeScript

S.No. Variable Declaration Syntax & Description
1.

var name:string = “tutorialspots”

The variable stores a value of type string

2.

var name:string;

The variable is a string variable. The variable’s value is set to undefined by default

3.

var name = “tutorialspots”

The variable’s type is inferred from the data type of the value. Here, the variable is of the type string

4.

var name;

The variable’s data type is any. Its value is set to undefined by default.

Example:

var string1:string = "tutorialspots"
var number1:number
number1 = 100
var number2
number2 = 200
var number3 = 30.5
var sum = number1 + number2 + number3
console.log("first string: "+string1) 
console.log("first number: "+number1) 
console.log("second number: "+number2) 
console.log("third number: "+number3) 
console.log("sum of the numbers: "+sum)

After compiling:

var string1 = "tutorialspots";
var number1;
number1 = 100;
var number2;
number2 = 200;
var number3 = 30.5;
var sum = number1 + number2 + number3;
console.log("first string: " + string1);
console.log("first number: " + number1);
console.log("second number: " + number2);
console.log("third number: " + number3);
console.log("sum of the numbers: " + sum);

Result:

[LOG]: first string: tutorialspots 
[LOG]: first number: 100 
[LOG]: second number: 200 
[LOG]: third number: 30.5 
[LOG]: sum of the numbers: 330.5 

Type Assertion in TypeScript

Syntax:

var var2:T = <T> var1

Example:

var var1 = '1' 
var var2 = <any> var1
var var3 = <number> var2
console.log(typeof(var3))

After compiling:

var var1 = '1';
var var2 = var1;
var var3 = var2;
console.log(typeof (var3));

Syntax 2:

var var2 = var1 as T

Example:

var var1 = '1'
var var2 = var1 as any
var var3 = var2 as number
console.log(typeof(var3))

Inferred Typing in TypeScript

var str = "tutorialspots";    // data type inferred as string

TypeScript Variable Scope

  • Global Scope − Global variables are declared outside the programming constructs. These variables can be accessed from anywhere within your code.
  • Class Scope − These variables are also called fields. Fields or class variables are declared within the class but outside the methods. These variables can be accessed using the object of the class. Fields can also be static. Static fields can be accessed using the class name.
  • Local Scope − Local variables, as the name suggests, are declared within the constructs like methods, loops etc. Local variables are accessible only within the construct where they are declared.

Example:

var global_var = 2         //global variable 
class Animal { 
   name = "Alex"           //class variable 
   static legs = 4         //static field 
   
   setName(new_name:string):void { 
      var name = new_name  //local variable 
      this.name = name
   } 
} 
console.log("Global var: "+global_var)  
console.log(Animal.legs)   //static variable  
var obj = new Animal() 
obj.setName("Milu")
console.log("Class var: "+obj.name) 

After compiling

var global_var = 2; //global variable 
var Animal = /** @class */ (function () {
    function Animal() {
        this.name = "Alex"; //class variable 
    }
    Animal.prototype.setName = function (new_name) {
        var name = new_name; //local variable 
        this.name = name;
    };
    Animal.legs = 4; //static field 
    return Animal;
}());
console.log("Global var: " + global_var);
console.log(Animal.legs); //static variable  
var obj = new Animal();
obj.setName("Milu");
console.log("Class var: " + obj.name);

Result:

[LOG]: Global var: 2 
[LOG]: 4 
[LOG]: Global var: Milu 

Leave a Reply