What is the difference between let and var in Typescript

What is the difference between let and var in Typescript

In this Post We Will Explain About is What is the difference between let and var in Typescript With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to What are let, const, var and the data types in TypeScript Example

In this post we will show you Best way to implement What are let, const, var and the data types in TypeScript, hear for TypeScript: Difference between Var, Let, and Const keyword with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

‘let and ‘var’ are the same in Typescript

make a file main.tsmain.ts

function SimpleDataPrint(){
	for(var i=0; i<5; i++){
		console.log(i);
	}

	console.log('Live last value:' + i);
}
SimpleDataPrint();

And then compile and simple step to run the compiled source code like:

tsc main.ts | node main.js

Output will be:

0
1
2
3
4
Live last value: 5

here value of print display i at the end is 5.

This is the major issue of declaring a simple variable with var data keyword. we have to simple declared value of like as a i inside the for data block so it is also new created var available outside the for some block or it is here available inside block the whole some function body.

if We declare or init the simple variable with let Like keyword.like:

function SimpleDataPrint(){
	for(let i=0; i<5; i++){
	console.log(i);
	}

	console.log('last value:' + i);
}

SimpleDataPrint();

It will immediately give a compilation error say 'can not find name i' as well as you will catch this error at some compile time. This is the more beauty of simple Typescript.

After then if We still some compile this file:

tsc main.ts

It will some give the error : Like as a 'can not find name i'.

Therefor it will still some generate dir like as the file main.js with the javascript source code.

function SimpleDataPrint(){
	for(var i=0; i<10; i++){
	console.log(i);
	}

	console.log('last value:' + i);
}

So this javascript source code is perfectly syntex valid javascript source code so now if you output run this simple like:

node main.js

it will give the same output:

0
1
2
3
4
5
6
7
8
9
last value: 10

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details......
Angularjs Example

Example

I hope you have Got What is TypeScript: Difference between Var, Let, and Const keywords And how it works.I would Like to have FeedBack From My Blog(Pakainfo.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(Pakainfo.com) Are Most Always Welcome.

Leave a Comment