JavaScript Let vs Var vs Constant | Mosh
VloĆŸit
- Äas pĆidĂĄn 14. 05. 2018
- JavaScript Let vs Var vs Constant -
đ„Get the COMPLETE course (83% OFF - LIMITED TIME ONLY): bit.ly/2KZea52
Subscribe for more videos:
/ @programmingwithmosh
Want to learn more from me? Check out my blog and courses:
programmingwithmosh.com
/ programmingwithmosh
/ moshhamedani
1) "var" is function-scoped, whereas "let" is block-scoped.
2) Using "var" attaches the variable to the window, whereas "let" prevents it.
for 2nd point , i think we dont have window object so how does it attaches to it?
plz correct me if im wrong, im just beginner
@@Cool94_4u are you a beginner now I started 3 month ago and your post was 3 month so I just want to know where you are right.
also, const is also block-scoped like "
let"
@@Cool94_4u
you are bigineer so why you are doing finger bro in hot burner .
@@Cool94_4u node uses "global" instead of "window" object
Mosh is the best teacher, calm and friendly voice, perfect speed and flow of speach. There are only a few software teacher, who increases my mood when following the tutorials and he is one of them!
I cannot agree more with you! Mosh is the best!
@@programmingwithmosh I have seen your full sql course
It's just awesome
yes, he nails it: just the right amount of info, nice pace; creating an easy to understand yet densely packed and concise lesson.
Finally, I understood the difference between var and let. Great video.
Short and to the point explanation! Thank you very much! I'm learning JS right now đ
I came here to learn about const. I left without that knowledge.
Here is the detailed explanation:
The 'const' is keyword what we use to declare variables with immutable value. Therefore a const type variable must be initialized during declaration because you can't modify its value later. But this is not true in every cases. For example: If you declare a const type variable which is an object actually you can modify its properties' value and you can add and remove properties (elements) from it, but can't replace the data type of this variable. So you can't change the object to an array. Don't forget: it will stay an object until it is alive. (Same situation with the arrays)
Const has exactly same scope as 'let' so it has block scope. Therefore it is available between the (next) curly braces where you declared. You can't re-declare it and you can't re-assign (update) its value. The hoist is working with const but it isn't initialized.
Go back to it and Google all the terminology in videos that will make a great difference sir.
Will if you declare something with const then you can't change the value in the entire program. Whatever you declare in const it will be constant throughout the code
Late, but const is just let but you canât change the value afterwards.
The point is this video is talking about let and var not const
I was searching everywhere to understand the difference and finally found this video. Really helpful! Thanks for making this video. :D
The best explanation of let and var keyword so far, Thanks
Thank you so much, that was crystal clear.
What a wonderful way to explain. loved it.
Packed with information, but short and concise. great video
CONST is a 'variable' that cannot be changed. It is a CONSTant. This is useful as a control so that you don't inadvertently make a change to a variable. You can define all your variables as CONST and change them as needed if you find you do need to update them.
_useState entered the chat_
Clear and straight to the point
Thanks for this video. Finally understood the difference between var and let
The best explanation ever! Thank you very much!
first time watching your video, great explanation! Thank you for your help!
Finally I understood the differance thanks Mosh !
Awesome. I understood very well.
A great and simple explanation of var vs. let. Thank you Mosh.
Great content loko. I subscribed by the 3rd video.
Great tutorial, I've more understand with it. Thank you
thanks :D quick and well explained!
This video was well done. Mosh, you tha man
helpful information thank you
love it SIr thank you
Super helpful you explained it very well.
You are awesome Mosh!!!
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?
Variables in Python also have the same scope as var in JS. I never thought of it as a real problem but I agree that it can sometimes result in bugs.
Thank you for pointing it out!
I experimented with the Python code below and it worked.
def start():
if x >= 21:
print("You can drink.")
else:
print("You can't drink.")
x = 23
start() # You can drink.
However, I found one interesting thing to notice the subtle difference.
def start():
for i in range(5):
print(i)
print(i)
start()
# Output :
0
1
2
3
4
4
The difference I found is logically same-written JavaScript code showed "5" at the last, but Python showed "4".
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 :)
This was an indepth ans to the question.
Thanks Mosh â€â€
Very good and clear explaination my friend!!
i was not good at all in the variables but now im proud of myself thank MR mosh
Mosh, You are the best man
I bought his js course recently. Iâve tried several courses and can honestly say this is one of the best.
His explanations are very clear, for the most part. I love his explanations and upbeat attitude.
I just wish there was some way to ask questions. There is no area for that on teachable, as there is on Udemy.
Can you give me the course free ?
Ayaz Hussain no
Thanks :)
What's the concern with var if you really need it althrough out the function scope?
Thanks Mosh!
ALOT of "javascript gurus" have all made 5-minute videos on the history and distinction between var and const and let, then NEVER actually DEFINED any of these terms, but instead launched into some over-caffeinated babble of code examples where utterly NOTHING was explained, but they're oh-so-proud of themselves.
YOU alone (so far) took the time to actually DEFINE these terms first: "const", "var", "let" and even "function" and "block" AND even "function-scoped" and "block-scoped". Then you examples were simple enough to make your target audience - PEOPLE NEW TO JAVASCRIPT WHO DON'T UNDERSTAND SOME OF THE CONCEPTS - actually understand the distinction between "function-scoped" and "block-scoped" variables.
Well done you - you just EARNED yourself another subscriber.
Cheers,
-Mark Vogt in North Aurora IL (USA)
Waste of time typing this longđ
Marvelous bro marvelous
god tier explanation thank you, sm
Thank you!
Hi, what color theme are you using for VSCode? Thanks
The reference point to the Window object is quite significant. It may cause leaks and overrides. Good thing to know.
thanks a lot
Where can I find more info about the function encapsulation? Now I'm curious
You didn't explain what const does.
same as let => block scoped
@@skalippanbalippan6972 nop. const is a constant, it's not a variable.
@@insideTheMirror_
It is a variable. With immutable binding not value.
@@philheathslegalteam Well I am not God here. Unlike you ;)
Here is the detailed explanation:
The 'const' is a keyword what we use to declare variables with immutable value. Therefore a const type variable must be initialized during declaration because you can't modify its value later. But this is not true in every cases. For example: If you declare a const type variable which is an object actually you can modify its properties' value and you can add and remove properties (elements) from it, but you can't replace the data type of this variable. So you can't change the object to an array. Don't forget: it will stay an object until it is alive. (Same situation with the arrays)
Const has exactly same scope as 'let' so it has block scope. Therefore it is available between the (next) curly braces where you declared. You can't re-declare it and you can't re-assign (update) its value. The hoist is working with const but it isn't initialized.
How do I get this view with VSCode and live output to Chrome console? Is this simply tied to an HTML file that's not being shown?
Hi Mosh,
what is the name of your VSCode theme?
Global and private initialization. Good stuff
THANKS!
Great video
nice explanation ..
You spoke at 5:37 about function encapsulation/modules and another video, that will explain this concept. I didn't find this video? Can you please link it or i have to buy the full course first?
Thanks in advance
Thanks a lot
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?
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
let variables cannot be read/written until they have been fully initialized, which happens when they are declared (if no initial value is specified on declaration, the variable is initialized with a value of undefined).
This differs from var variables, which will return a value of undefined if they are accessed before they are declared.
Seeing the examples it's all clear... We must NEVER use var. Amazing how easy you make it look
Hi Mosh please make tutorial on spring boot and especially spring security .It will be very useful for lots of people , especially of you are doing :)
I have a question. I am a UI dev for a web based application company. Now majority of our clients are using very old machines and old browsers which were not updated. If I start converting my JS code from ES5 to ES6, will it cause issues running in those old computers? What is the solution?
I know it is VERY confusing because var and let are EXTREMELY THE SAME
But they are a difference
That for example, var x="john" ;
x is defined in your whole code, not only in this block
And you may change it depends on how you want and you can't use x in other functions
let x="John"
Defines x only in your code block
And you can only make it work by putting the method for example console.log inside of the curly braces of the function they're in it
I have to commend you. I have searched for the use of the Var or Let keyword. Currently am ongoing a course but got curious as to why in my course there is only Let and no use of Var nor Conts.
Really good tutorials!! You should make some courses on udemy! Thank you very much
Great video but it would have been nice to have it numbered since it appears to be part of a series.
what if i want to declarate global variable inside of function?
well explained
Hi Sir!! You said that the variables declared with let keyword is not attached to window object, then where is is stored ? If it is stored is script scope, can you explain the script scope as global scope?
Love You bro
Sir can you please make a video on Closures in JS..
So for, for loops, I should still use var?
Whats your color theme in VS Code ??
Mosh is the best!!!
so should we use them in some cases or avoid using them forever
Finally. I got it.
Very clear explanation, but I don't understand the why var is a problem at 3:13
Awesome...you explained it so simple. Finally, I got the difference!
how to enable window and global object in node environment?
thx
After watching this and searching a lil bit, i think it's wrong to try to use let for everything, since var is function-scoped and let it's block-scoped so you should use both and not only one, like two for inside of one you need to know the two loops indexs and using let you'll not have this information and not even any result outside of them which is bad since we may need the result of others scope functions inside others functions, we don't have thoose "problems" using C# but i don't agree with using let in every situation, it may give even more bugs than just using var which is like most of the languages work which was what a lot of people were saying about let, let should be only used when you know that you'll not need that variable anymore outside of that situation, btw amazing tutorial, js it's preety cool.
global variables and function-scoped variables;
What theme is he using?
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.
const is also block-scoped, but It can't be reassigned. You can modify them but can't reassign them.
10/10 your performance
Hi sir,I have a doubt when using const keyword value will not change but when I use like this value is changing for 'const' keyword. code is: const x = { name: "scott"}; x.name = "john"; console.log(x); Result:john. Why const is printing john instead of scott. Can you please give me reason.
its uniqueee
For some reason
var apple = 'apple';
console.log(window.apple)
throws undefined, like It is not attaching apple to the window object..
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?
Yes. Better Encapsulation.
The question is where do those variables declared using let or const go?? Somewhere in the memory?
Yes, JavaScript are stored in two places: stack (local context) and heap (store dynamically).
You can subscribe our new post or follow us on speedysense.com/var-vs-let-vs-const-in-javascript/
what is his apps ? what name of that apps? i
I don't have time to watch all the other lectures or watch till I find what he said so can someone tell me how to prevent a 'function' from being added into window? Thanks in advance
let sayHi =function() { console.log('hi');}
Nice one my friend đđđ I understood what you were communicating
What's the difference between using âletâ and âvarâ?
3:35 I dont know why is that a problem. I mean I know when the codes get complex it will be hard to manage the window stuff but sometimes if it's neccesary to use it, it can be used in my opinion
why we even have var then? What's their purpose/adventage of it if any
Please put this course in udemy. I love your videos. You're the best
no const :(
but this video helped me little bit, so thanks anyway :)
What about constant?
New access modifiers are succeeding Javascript
I think you forgot the constant from your title :)
I still use var
what if you just used
test = 'color';