Difference between var,let and const in java script and typescript
-----------------------------------------------------------------------------------
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
D:\TypeScriptProject>tsc main.ts
D:\TypeScriptProject>node main.js
typescript
20
30
10
-----------------------------------------------------------------------------------
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