• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
projectsgeek

ProjectsGeek

Download Mini projects with Source Code, Java projects with Source Codes

  • Home
  • Java Projects
  • C++ Projects
  • VB Projects
  • PHP projects
  • .Net Projects
  • NodeJs Projects
  • Android Projects
    • Project Ideas
      • Final Year Project Ideas
      • JSP Projects
  • Assignment Codes
    • Fundamentals of Programming Language
    • Software Design Laboratory
    • Data Structure and Files Lab
    • Computer Graphics Lab
    • Object Oriented Programming Lab
    • Assembly Codes
  • School Projects
  • Forum

Difference between VAR and LET Angular 2

September 23, 2017 by ProjectsGeek Leave a Comment

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

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

    console.log('Finally ' + i);
}


doSomething();

Run this below command to compile type script

PS C:\Users\AnilKumar\ts-hello> tsc code-main.ts

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

PS C:\Users\AnilKumar\ts-hello> node code-main.js
0
1
2
3
4
Finally 5
PS C:\Users\AnilKumar\ts-hello>

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.

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

    console.log('Finally ' + i);
}


doSomething();

Again compile the code as we did earlier.

PS C:\Users\AnilKumar\ts-hello> tsc code-main.ts
code-main.ts(9,30): error TS2304: Cannot find name 'i'.

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

 

 

Other Projects to Try:

  1. Public and Private Keywords
  2. Set operations – Union, Intersection, Difference, Symmetric Difference using C
  3. Search Result
  4. Declaring Interface in TypeScript Angular 2
  5. Declaring Classes in TypeScript Angular 2

Filed Under: Angular 2 Tagged With: Angular 2

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tags

.Net Projects Download Android Project Ideas Android Projects Angular 2 Assembly Codes C # Projects C & C++ Projects C++ Projects Class Diagrams Computer Graphics Database Project Data Mining Projects DataScience Projects Datastructure Assignments Download Visual Basic Projects Electronics project Hadoop Projects Installation Guides Internet of Things Project IOS Projects Java Java Interview Questions Java Projects JavaScript JavaScript Projects java tutorial JSON JSP Projects Mechanical Projects Mongodb Networking Projects Node JS Projects OS Problems php Projects Placement Papers Project Ideas Python Projects seminar and presentation Struts

Search this Website


Footer

Download Java Project
Download Visual Basic Projects
Download .Net Projects
Download VB Projects
Download C++ Projects
Download NodeJs Projects
Download School Projects
Download School Projects
Ask Questions - Forum
Latest Projects Ideas
Assembly Codes
Datastructure Assignments
Computer Graphics Lab
Operating system Lab
australia-and-India-flag
  • Home
  • About me
  • Contact Form
  • Submit Your Work
  • Site Map
  • Privacy Policy