Web Programming TutorialsDeclaring a Variable in TypeScript

Declaring a Variable in TypeScript

A tailor made description of a variable is, “namespace in a memory, used to store values.”! Variables are the most vital part in any language and consist of specific functionalities. In general, variables must be declared before it is used, and it has to be followed by the var keyword, to declare variables. This is the general representation of a variable and now we will describe the variable declaration in TypeScript.In TypeScript variables have to follow JavaScript naming rules, while declaration. In this context, we have mentioned the two very basic naming rules as below:

  • In this a variable name can contain alphabets and numeric digits.
  • Variables cannot contain any spaces, and special characters, except the dollar ($) and the underscore (_) sign.
  • A variable names cannot begin with a digit.

Declaring a Variable in TypeScript:

While declaring a variable we have four main options. In TypeScript the type syntax for declaring a variable is to include a colon (:) after the variable name, followed by its type. So, to follow the main four declaring options we have:

  • Declare the type and value in one statement.
Var [identifier] : [type-annotation] = value ;
  • Declaration the type but no value. In this case, the variable will be set to undefined.
Var [identifier] : [type-annotation] ;
  • The third type declares the value but no type, the variable type will be set to any.
Var [identifier] = value ;
  • Declare neither value nor type. The data type of this variable will be any and will be initialized to undefined.
Var [identifier] ;

Now to describe this whole var (variable) declaration in detail, we have maintained a table.

Variable declaration syntax

Variable syntax description

var name;

The result for this variable’s data type will be any. Its value is set to undefined by default.

var name:string;

The result for this variable is a string variable. So, the result for this variable’s value is set to undefined by default.

var name = ”mary”

This variable’s type is inferred from the rest data type of the value. So, this variable is of the type string

var name:string = ”mary”

The declaration of this variable will stores a value of type string

Few examples of declaring a variable:
var a = 10;

Now to declare a variable as a function;

function f() {
    var message = "Hello, world!";

    return message;
function f() {
    var message = "Hello, world!";

    return message;

After (var) there is one another variable used instead of var, which is let. Let statement was introduced, because var has some complication issue. Although let statement has written in the same way as var, but the key difference is semantic than syntax.

Now, let’s have a look on its syntax and its benefits over the var

let hello = "Hello!";

So, to begin with, the one benefit is block-scoping; this is useful in blockage the leak of scope in their containing functions and blocks, and this is not available in var!

TypeScript Variable Scope:

The scope is the major part in all these programming, which is why the availability of a variable is determined by its scope. The scope of a variable is specified where it need to be defined and not everywhere.

Local scope:
Local variables are accessible within the construct, they have declared.

Class scope:
Class variables are also known as fields, use to declare within the class but outside the method. The Class is also static type and these static fields can be accessed by class name methods.

Global scope:
These global variables are used to declare outside the constructs and more from all as the name suggest, this variable can be accessed from anywhere within the code.

Example: Variable Scope

var global_num = 12          //global variable 
class Numbers { 
   num_val = 13;             //class variable 
   static sval = 10;         //static field 
   storeNum():void { 
      var local_num = 14;    //local variable 
console.log("Global num: "+global_num)  
console.log(Numbers.sval)   //static variable  
var obj = new Numbers(); 
console.log("Global num: "+obj.num_val)

On compilation following javascript code has been generated:

var global_num = 12;              //global variable 
var Numbers = (function () {
   function Numbers() {
      this.num_val = 13;          //class variable 
   Numbers.prototype.storeNum = function () {
      var local_num = 14;        //local variable 
   Numbers.sval = 10;            //static field 
   return Numbers;

console.log("Global num: " + global_num);
console.log(Numbers.sval);       //static variable  

var obj = new Numbers();
console.log("Global num: " + obj.num_val);

So, the result will be as follow:

Global num: 12
Global num: 13

In another case, if one will try to compile the local variables outside this method, then it can show a compilation error for sure.

We hope this blog will help you out in understanding the main functions and features of variables and their declaration with types. For more details, please ask us your queries!


Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article


More article

- Advertisement -Eduonix Blog