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/
Tweets by moshhamedani
Original source
39 responses to “JavaScript Let vs Var vs Constant | Mosh”
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
A fucking rant about fucking const vs fucking let :
https://jamie.build/const
Disliked video because you didn't explain const
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.
So using let is more secure? I’m curious, you would only want to limit the variables scope to only where it will be used/accessed?
Clear and straight to the point
thanks 😀 quick and well explained!
What a wonderful way to explain. loved it.
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?
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.
What's the difference between using “let” and “var”?
<a href="https://stackdev.io/question/1010/whats-the-difference-between-using-let-and-var-1"
Hi, what color theme are you using for VSCode? Thanks
Hi Mosh,
what is the name of your VSCode theme?
Where can I find more info about the function encapsulation? Now I'm curious
Mosh is the best!!!
I am very curious how that loop at 1:30 managed to count to 5.
Bro….
I came here to learn about const. I left without that knowledge.
What's the concern with var if you really need it althrough out the function scope?
Awesome. I understood very well.
Thanks a lot <3
Sir can you please make a video on Closures in JS..
nice explanation ..
lmao horrible, doesnt even cover Constants as per the title.
Thank you so much, that was crystal clear.
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 🙂
let sayHi =function() { console.log('hi');}
You didn't explain what const does.
Hi Mosh. If var attaches variables to the window/global object where are those declared with let/const attached to if they're not declared inside a block? Thanks, great videos btw
More like just "var vs let" than "var vs let vs const"…
what if you just used
test = 'color';
Awesome…you explained it so simple. Finally, I got the difference!
You are awesome Mosh!!!
Finally. I got it.
Great video, thank you. So if I declare a variable using let in the global scope, does that have the same scope as the window object?
The award for lost clear explanation ever goes to you sir!
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!
I will still be using var… but still thanks for the explanation… :]
The best explanation of let and var keyword so far, Thanks
10/10 your performance