JavaScript Let vs Var vs Constant | Mosh




JavaScript Let vs Var vs Constant –

🔥Get the COMPLETE course (83% OFF – LIMITED TIME ONLY): http://bit.ly/2M1sp4B

Subscribe for more videos:
https://www.youtube.com/channel/UCWv7vMbMWH4-V0ZXdmDpPBA?sub_confirmation=1

Want to learn more from me? Check out my blog and courses:

http://programmingwithmosh.com
https://www.facebook.com/programmingwithmosh/


Original source


39 responses to “JavaScript Let vs Var vs Constant | Mosh”

  1. What a explanation boss!! I have been cleared the let vs var. Before watching the video i was in a confusion about that. But now i have been cleared that for your great tutorial. God bless you. Thanks

  2. In addition to everything mentioned in this video, let has reassignment protection. What I mean by this is the following:

    If you declare a variable:

    var test = 'test';

    and later have:

    var test = 'new test';

    then the variable test will be reassigned, sometimes without your knowledge.

    with let, this does not happen. If you declare:

    let test = 'test';

    then later:

    let test = 'new test';

    You'll hit a error/warning that test has already been declared. Unlike a const however, it can still be reassigned the 'normal' way, which is much more intentional:

    test = 'new test';

    because you are not using let before the name of the variable, it is clear that you know the variable already exists, and you are reassigning it. This protection helps prevent accidental reassigning of a variable you already declared but then may have forgotten about, and reduces debugging. Yet another reason to use let instead of var!

    note – const cannot be reassigned at all, even using the 'deliberate' way shown above, it is not a variable, but a constant. It does not change.

  3. I hate to sound stupid but why if the loop terminates at i < 5 is the sixth var 5 and not 4? I thought the loop terminate. Does this mean the i++ is the last bit of information at the end of the loop however it wouldn't display it on the console? (meaning the value of i at 5 is assigned but not printed?

  4. I'm at complete beginner level, been learning only for a couple hours. So I don't know enough to know why you would even want to have variables be block scoped instead of having all variables be global and each of them having a different name. To me seems like the latter would be easier to work with so I can remember what each variable does better but I since i'm so new I will just trust you.

  5. Mosh jan, you should add WHY bloating the "window" let say or other objects with variables and functions is bad. watching your video, a novice would think, "well, why would having access to data outside of a scope bad?!", which is a valid and good question. Otherwise, a very nice video, thanks 🙂

  6. I'm learning JavaScript and your videos answer many questions, in a concise manner, that I still have after many months. Thank you for posting these on YouTube and helping us all become better! I appreciate it!

Leave a Reply