In this blog we can see variables in TypeScript .

Variables in TypeScript : 

TypeScript variables can be referred to by a name called an identifier and are designated storage sites to hold a type of or dynamic kind of value.

Syntax : 

var <identifier> [: type-annotation] [= value] ;

Scope of variables :

The scope is a particular section of code where a variable can be accessed. According to the scope, there are three categories of variables.

1. Global Scope :

Variables that are not class-specific and can be accessed from anywhere in the code.

2. Class Scope :

These variables are class members and can be accessed by class member functions.

3.Local Scope :

Variables declared inside a construct and destroyed once control is removed from that construct. The construct can be a loop, function, if block, case block, and so on.

 

StaticVariables : 

Static variables, like those used in Java, are not bound to any objects and can be accessed by class name. Their value can be changed, and the state change is saved throughout the program’s life cycle.

Example : 

var n1:number = 238
class Person {
name:string
static sval = 20
printSomething():void {
var message:string = ‘Hi!’
}

For any Help or Queries Contact us on info@crmonce.com or +918096556344