Difference between var and let in TypeScript

2
362
CoderFunda

Introduction:
Their are some java script errors like consider the following example.

class difference {
    getVar(): void {
        var a = 1010;
        if (true) {
            var a = 2020;
        }
        alert("You are in var function: "+ a); // 2020
    }
}

var obj = new difference();
obj.getVar();

In above example you will get result as 2020. Because { } this block does not create a new variable scope in if condition. The variable is remains same as it is.

Your output will be

let keyword :

The let keyword is actually a newer JavaScript construct that TypeScript makes available. TypeScript (and ES6) introduces the let keyword.

What is ES6?

  • ECMAScript = ES:
  • ECMAScript (or ES) is a trademarked scripting-language specification standardized by Ecma International in ECMA-262 and ISO/IEC 16262.
  • It was created to standardize JavaScript, so as to foster multiple independent implementations. JavaScript has remained the best-known implementation of ECMAScript
  • Languages like Javascript are based on the ECMAScript standard.
  • ECMA Standard is based on several originating technologies, the most well known being JavaScript (Netscape) and JScript (Microsoft).
  • ECMA means European Computer Manufacturer’s Association

For more info https://en.wikipedia.org/wiki/ECMAScript

Consider below let block.

class difference {

getLet(): void {
        let b = 1010;
   //let b = 2525;    // you can not re-declare b in same scope
        if (true) {
            let b = 2020;
            alert("You are in if condition: " + b); // 1010
        }
        alert("You are in let function: " + b); // 1010
    }
}

After compilation of code into java script as bellow

var difference = (function () {
    function difference() {
    }    
    difference.prototype.getLet = function () {
        var b = 1010;
        //let b = 2525;    // you can not re-declare b in same scope
        if (true) {
            var b_1 = 2020;
            alert("You are in if condition: " + b_1); // 1010
        }
        alert("You are in let function: " + b); // 1010
    };
    return difference;
})();
var objDiff = new difference();
objDiff.getLet();

In this above case for each let keyword will has a unique elements depends upon a condition i.e if block is true then it will create unique element & in alert you will get result as.

Summary :
In this article you learn difference between var and let in TypeScript

2 COMMENTS

  1. Hello ,

    I saw your tweets and thought I will check your website. Have to say it looks very good!
    I’m also interested in this topic and have recently started my journey as young entrepreneur.

    I’m also looking for the ways on how to promote my website. I have tried AdSense and Facebok Ads, however it is getting very expensive.
    Can you recommend something what works best for you?

    Would appreciate, if you can have a quick look at my website and give me an advice what I should improve: http://janzac.com/
    (Recently I have added a new page about FutureNet and the way how users can make money on this social networking portal.)

    I wanted to subscribe to your newsletter, but I couldn’t find it. Do you have it?

    Hope to hear from you soon.

    P.S.
    Maybe I will add link to your website on my website and you will add link to my website on your website? It will improve SEO of our websites, right? What do you think?

    Regards
    Jan Zac

    • Hello Jan,
      Still we have not developed the subscribe to your newsletter. And about promotion & branding you need to use social media which is a FREE of cost for everything. Increase your followers from Facebook, twitter etc. One my friend had developed the whole website. I can do one thing, will give you contact details, He will help you to generate revenue from your website & will help you for branding as well.

      You can join me on facebook so will be in touch.

      Regards,
      Rupesh Kahane
      +91 9545273748 (Whatsapp no)

LEAVE A REPLY

Please enter your comment!
Please enter your name here