Variable Types in TypeScript

TypeScript supports various types of values like a number, string, etc and to store them there are a few numbers of variables also,

In TypeScript here is 6 type of variable in typescript and they are as described below,

  • Number:
    To declare a number type variable first use the keyword let or var then the variable name and then the keyword number followed by a colon.

Example:

var a_num: number = 10;
console.log(a_num);

// Note: There is no separate concept of integer or float type variables in TypeScript, for both types of values we can use number.

  • String:
    A string type variable is used to store string values and to define a string type variable first use the keyword let or var then the variable name and then the keyword string followed by a colon.
    Example:

    var full_name: string = ‘John Doe’;
    console.log(full_name);
    //Note: String is used to store any sequence of caracter.

  • Boolean:
    The boolean type variable is used to store true or false, and to define a boolean type variable first use the keyword let or var then the variable name and then the keyword boolean followed by a colon.
    Example:
    var bool_val: boolean = true;
    if(bool_val){
    console.log(‘Boolean is true’);
    }else{
    console.log(‘Boolean is false’);
    }
    //Note: The boolean is used for logical value true or false

  • Void:
    The void is used when a function is not retarnable.
    Example:
    function function_name():void {
    //statements
    }
  • Null:
    The null type of variable is used to initialize a veriable with null value.
    Example:
    var a_null_variable: null;
    //Note: Unbdefined denotes value given to all uninitialised veriable.

Different between null and undefined

The null and the undefined datatypes are often a source of confusion. The null and undefined cannot be used to reference the data type of a variable. They can only be assigned as values to a variable.

However, null and undefined are not the same. A variable initialized with undefined means that the variable has no value or object assigned to it while null means that the variable has been set to an object whose value is undefined.

  • Any:
    Any type variable is used to store any type of variable after defining the variable.
    Example:

    var a_any_type_variable:any;
    a_any_type_variable = ‘Storing string in any type variable’;
    console.log(a_any_type_variable);

    Accept the variables there is some other variable types also like Enumerations (enums), classes, interfaces, arrays, tuple.
    We are going to discuss the Enumerations (enums), classes, interfaces, arrays, and tuple in later.

Share The Post On -

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.