Thursday, 6 June 2019

Type Script variables

Difference between var,let and const in java script and typescript
-----------------------------------------------------------------------------------
<script>
        // calling x after definition
        var x = 5;
        document.write(x, "\n");
  
        // calling y after definition 
        let y = 10;
        document.write(y, "\n");
  
        // calling var z before definition will return undefined
        document.write(z, "\n");
        var z = 2;
  
        // calling let a before definition will give error
        document.write(a);
        let a = 3;
    </script>


Hoisting in JavaScript
----------------------------

<script>

x=10;

var x;

document.write(x);// 10

</script>

Note: Type script wont support hoisting 

Type script 
--------------

Variables are declared by using any of the below keywords
1.var
2.let
3.const

1.var:
- it is used to define function scope variable.
- it is accessible any location within the function.
- it can be rendered with different values.
Example:
     
          var x=10;
                x=20; // valid

it can be shadowed
           var x= 10;
           var x= 20; // valid

var x:number = 10;
x=20; // it can be rendered different values
console.log(x); // 20

var x:number =30;// it can be shadowed
console.log(x); // 30

let y:number =10
let y:number =20; //error(can't redeclared) - it cant be shadowed

console.log(y);// it can be redndered different values



D:\TypeScriptProject>tsc main.ts

D:\TypeScriptProject>node main.js

typescript
20
30
10

No comments:

Post a Comment

Type Script variables

Difference between var,let and const in java script and typescript -----------------------------------------------------------------------...