Declaring a Variable in TypeScript

0
35
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.

  • Declaration the type but no value. In this case, the variable will be set to undefined.

  • The third type declares the value but no type, the variable type will be set to any.

  • Declare neither value nor type. The data type of this variable will be any and will be initialized to undefined.

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;

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

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

On compilation following javascript code has been generated:

So, the result will be as follow:

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!

LEAVE A REPLY

Please enter your comment!
Please enter your name here