Variables in TypeScript

There ia a number of structure to define a variable in typescript and we are going to discuss upon that,

  • var/let [variable name] : [type of the variable] = [value]

    In this case first have to write the keyword ver or let then the variable name and after that have to write the variable type keyword followed by a colon and then you can assign a value on it providing an equal sign before the value.

    Example:

    var first_name:string = ‘Code’;
    let second_name:string = ‘Mystery’;
    console.log(‘Full Name: ‘ + first_name + ‘ ‘ + second_name);
  • var/let [variable name] : [variable type] ;

    Its a procedure to declare a variable but here we are not assigning any value at the time of declaration but can assign value after the declaration of the variable. The procedure to declare is first written the keyword ver or let then the variable name and after that have to write the variable type keyword followed by a colon.

    Example:

    var first_name:string;
    let second_name:string;
    first_name = ‘Code’;
    second_name = ‘Mystery’;
    console.log(‘Full Name: ‘ + first_name + ‘ ‘ + second_name);
  • var/let [variable name] = [value];

    Its a similar process we are using in javascript and for that first have to use let or var to make typescript understand that the next one is a variable name and then the name of the variable and then the value after providing an equal sign.

    Example:

    var first_num = 10;
    var second_num = 20;
    var sddition_res = first_num+second_num;
    console.log(sddition_res);
  • var/let [nariable name] ;

    Its a process to declare a variable without assigning a value in it at the time of declaration. to declare that first have to use let or var to make typescript understand that the next one is a variable name and then the name of the variable.

    Example:

    var first_num;
    var second_num;
    var sddition_res;
    first_num = 10;
    second_num = 20;
    sddition_res = first_num+second_num;
    console.log(sddition_res);

Note: In case of number 3 and number 4 the type of the variable will be any as by default typescript set the variable type as any if no type is defined there at the time of declaration.

Examples:

 var int_number:number = 12; // number type variable in int
var flot_number:number = 15.52; // another number type variable
var sumation = int_number + flot_number; // Not declared a variable type but assigned the sum value of declared integer and float number
console.log(sumation); // The output of the sumation is 27.52
var first_name:string = 'Code'; // A string type variable
let second_name:string = 'Mystery'; // Another string type variable
full_name = first_name + ' ' + last_name; //Not declared the type of the veriable but it will store string value
console.log(full_name); // The output will be Code Mystery
console.log(extra_variable); //The veriable extra_variable is not defined
// The output of a never defined variable is error TS2304: Cannot find name 'extra_variable' which is a typescript error
// but it will generate a js file and there the error will be ReferenceError: extra_variable is not defined

Share The Post On -