Share with others

In this post we will see how var keyword in type script is different from let keyword. We will use very simple program to explain the difference between var vs let keywords in angular 2 typescript.

VAR Keyword Usage

We will write a simple program with var keyword as give below

Run this below command to compile type script

Run the code-main.js file using node.js

Variable ‘i’ is declared inside loop but still we can use it outside loop which make its available for other code as well inside doSomething() function.

LET Keyword Usage

We will change the keyword var –> let as given below.

Again compile the code as we did earlier.

We have compilation error which suggest variable ‘i’ is not visible outside loop that means scope is limited to nearest scope. We can this error in visual studio code as well when writing code.

You can see outside loop variable is marked error and you can see error message if you hover mouse pointer on it.

var and let Angular 2



Share with others