JavaScript Let vs Var vs Constant | Mosh

  Рет қаралды 284,418

Programming with Mosh

Programming with Mosh

6 жыл бұрын

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:
programmingwith...
/ programmingwithmosh
/ moshhamedani

Пікірлер: 186
@sanket_sy
@sanket_sy 4 жыл бұрын
1) "var" is function-scoped, whereas "let" is block-scoped. 2) Using "var" attaches the variable to the window, whereas "let" prevents it.
@Cool94_4u
@Cool94_4u 4 жыл бұрын
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
@Victory_Accomplished
@Victory_Accomplished 3 жыл бұрын
@@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.
@turbo6266
@turbo6266 3 жыл бұрын
also, const is also block-scoped like " let"
@deepakkumaryadav6009
@deepakkumaryadav6009 2 жыл бұрын
@@Cool94_4u you are bigineer so why you are doing finger bro in hot burner .
@harshagarwal00
@harshagarwal00 2 жыл бұрын
@@Cool94_4u node uses "global" instead of "window" object
@m.u.550
@m.u.550 6 жыл бұрын
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!
@forexdragon
@forexdragon 5 жыл бұрын
I cannot agree more with you! Mosh is the best!
@yutaitadori7318
@yutaitadori7318 3 жыл бұрын
@@programmingwithmosh I have seen your full sql course
@yutaitadori7318
@yutaitadori7318 3 жыл бұрын
It's just awesome
@plasmaastronaut
@plasmaastronaut Жыл бұрын
yes, he nails it: just the right amount of info, nice pace; creating an easy to understand yet densely packed and concise lesson.
@flyingbirds4235
@flyingbirds4235 6 жыл бұрын
Finally, I understood the difference between var and let. Great video.
@codinginflow
@codinginflow 2 жыл бұрын
Short and to the point explanation! Thank you very much! I'm learning JS right now 😁
@janus7156
@janus7156 5 жыл бұрын
You didn't explain what const does.
@skalippanbalippan6972
@skalippanbalippan6972 5 жыл бұрын
same as let => block scoped
@insideTheMirror_
@insideTheMirror_ 4 жыл бұрын
@@skalippanbalippan6972 nop. const is a constant, it's not a variable.
@philheathslegalteam
@philheathslegalteam 4 жыл бұрын
@@insideTheMirror_ It is a variable. With immutable binding not value.
@insideTheMirror_
@insideTheMirror_ 4 жыл бұрын
@@philheathslegalteam Well I am not God here. Unlike you ;)
@crepkey
@crepkey 4 жыл бұрын
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.
@frankvee
@frankvee 3 жыл бұрын
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.
@somedudeonyoutubefrfr
@somedudeonyoutubefrfr Жыл бұрын
_useState entered the chat_
@human151
@human151 6 жыл бұрын
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.
@ayazhussain7109
@ayazhussain7109 6 жыл бұрын
Can you give me the course free ?
@human151
@human151 6 жыл бұрын
Ayaz Hussain no
@ayazhussain7109
@ayazhussain7109 6 жыл бұрын
Thanks :)
@RezaMohseni
@RezaMohseni 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 :)
@VOGTLANDOUTDOORS
@VOGTLANDOUTDOORS Жыл бұрын
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)
@Eshwar_nigga
@Eshwar_nigga Жыл бұрын
Waste of time typing this long😅
@Markkkkkkkkkkkkkkkkk
@Markkkkkkkkkkkkkkkkk 4 жыл бұрын
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
@berniecat8756
@berniecat8756 5 жыл бұрын
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.
@jathebest2835
@jathebest2835 Жыл бұрын
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".
@SimarpreetKaur-er1dw
@SimarpreetKaur-er1dw 2 жыл бұрын
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.
@pranjalijambhule6729
@pranjalijambhule6729 4 жыл бұрын
I was searching everywhere to understand the difference and finally found this video. Really helpful! Thanks for making this video. :D
@hardik.motwani
@hardik.motwani 5 жыл бұрын
The best explanation of let and var keyword so far, Thanks
@iq0578
@iq0578 3 жыл бұрын
Very clear explanation, but I don't understand the why var is a problem at 3:13
@samithakulatilaka
@samithakulatilaka 5 ай бұрын
A great and simple explanation of var vs. let. Thank you Mosh.
@techlightdev
@techlightdev Жыл бұрын
Great tutorial, I've more understand with it. Thank you
@manuelcasares7270
@manuelcasares7270 3 жыл бұрын
For some reason var apple = 'apple'; console.log(window.apple) throws undefined, like It is not attaching apple to the window object..
@TheRonron1994
@TheRonron1994 5 жыл бұрын
What's the concern with var if you really need it althrough out the function scope?
@yuriaugusto1084
@yuriaugusto1084 2 жыл бұрын
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;
@driassasofiane2839
@driassasofiane2839 Жыл бұрын
i was not good at all in the variables but now im proud of myself thank MR mosh
@memaimu
@memaimu 4 жыл бұрын
I came here to learn about const. I left without that knowledge.
@crepkey
@crepkey 4 жыл бұрын
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.
@ciceroaraujo5183
@ciceroaraujo5183 4 жыл бұрын
Go back to it and Google all the terminology in videos that will make a great difference sir.
@charankumar9897
@charankumar9897 4 жыл бұрын
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
@semisubmersible
@semisubmersible 4 жыл бұрын
Late, but const is just let but you can’t change the value afterwards.
@Victory_Accomplished
@Victory_Accomplished 3 жыл бұрын
The point is this video is talking about let and var not const
@priyamganguly
@priyamganguly 3 жыл бұрын
The reference point to the Window object is quite significant. It may cause leaks and overrides. Good thing to know.
@mudasirh1404
@mudasirh1404 3 жыл бұрын
const is also block-scoped, but It can't be reassigned. You can modify them but can't reassign them.
@xxsaifxx2450
@xxsaifxx2450 4 жыл бұрын
so should we use them in some cases or avoid using them forever
@blub579
@blub579 3 жыл бұрын
Packed with information, but short and concise. great video
@Levelonesucks
@Levelonesucks 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.
@milindkhadse556
@milindkhadse556 5 ай бұрын
helpful information thank you
@chandankumarthakur7182
@chandankumarthakur7182 5 жыл бұрын
let sayHi =function() { console.log('hi');}
@Himanshu_Sharma..
@Himanshu_Sharma.. 4 жыл бұрын
Nice one my friend 👍👊👌 I understood what you were communicating
@dijiflex
@dijiflex 4 жыл бұрын
Clear and straight to the point
@braco0000
@braco0000 3 жыл бұрын
Seeing the examples it's all clear... We must NEVER use var. Amazing how easy you make it look
@Shiva-zy7jq
@Shiva-zy7jq 4 жыл бұрын
Thanks for this video. Finally understood the difference between var and let
@bigbadcatbigbcy2933
@bigbadcatbigbcy2933 6 ай бұрын
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
@evans_2103
@evans_2103 2 жыл бұрын
The best explanation ever! Thank you very much!
@maulikgevariya3059
@maulikgevariya3059 2 жыл бұрын
Finally I understood the differance thanks Mosh !
@pranavpatil5849
@pranavpatil5849 5 жыл бұрын
Sir can you please make a video on Closures in JS..
@kondasomu
@kondasomu Жыл бұрын
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.
@kicn
@kicn 4 жыл бұрын
What a wonderful way to explain. loved it.
@kevinpatel5106
@kevinpatel5106 4 жыл бұрын
first time watching your video, great explanation! Thank you for your help!
@theweb_developer
@theweb_developer Жыл бұрын
Super helpful you explained it very well.
@rupamsutar9299
@rupamsutar9299 Жыл бұрын
This was an indepth ans to the question. Thanks Mosh ❤❤
@gautambedi591
@gautambedi591 Жыл бұрын
Mosh, You are the best man
@rolypolyyy
@rolypolyyy Жыл бұрын
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?
@MrRossss1
@MrRossss1 5 жыл бұрын
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
@Brian-bo2fu
@Brian-bo2fu 5 жыл бұрын
The question is where do those variables declared using let or const go?? Somewhere in the memory?
@speedysense5061
@speedysense5061 5 жыл бұрын
Yes, JavaScript are stored in two places: stack (local context) and heap (store dynamically).
@speedysense5061
@speedysense5061 5 жыл бұрын
You can subscribe our new post or follow us on speedysense.com/var-vs-let-vs-const-in-javascript/
@znoran
@znoran 2 жыл бұрын
Very good and clear explaination my friend!!
@rohitkumarshrivastava9693
@rohitkumarshrivastava9693 3 жыл бұрын
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?
@mounirkanane8083
@mounirkanane8083 3 жыл бұрын
What about constant?
@ga7853
@ga7853 5 жыл бұрын
Thank you so much, that was crystal clear.
@petersuba1125
@petersuba1125 2 жыл бұрын
what if i want to declarate global variable inside of function?
@SamuelHauptmannvanDam
@SamuelHauptmannvanDam 3 жыл бұрын
So for, for loops, I should still use var?
@eduardohernandez552
@eduardohernandez552 6 жыл бұрын
Really good tutorials!! You should make some courses on udemy! Thank you very much
@pouyajabbarisani
@pouyajabbarisani 4 жыл бұрын
Hi Mosh, what is the name of your VSCode theme?
@gaminginn6072
@gaminginn6072 3 жыл бұрын
why we even have var then? What's their purpose/adventage of it if any
@mikepalazzo
@mikepalazzo 5 жыл бұрын
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?
@jerryrobinson958
@jerryrobinson958 3 жыл бұрын
This video was well done. Mosh, you tha man
@sumonchandrashil8406
@sumonchandrashil8406 5 жыл бұрын
Awesome. I understood very well.
@OfoeNelson
@OfoeNelson 3 жыл бұрын
Great video but it would have been nice to have it numbered since it appears to be part of a series.
@tochachochi
@tochachochi 5 жыл бұрын
Great content loko. I subscribed by the 3rd video.
@tracetv8115
@tracetv8115 3 жыл бұрын
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
@wotanman7711
@wotanman7711 4 жыл бұрын
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?
@naveedurrehman2931
@naveedurrehman2931 3 жыл бұрын
love it SIr thank you
@prashanttiwari120
@prashanttiwari120 3 жыл бұрын
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 :)
@petrchutny
@petrchutny 4 жыл бұрын
Thanks Mosh!
@LucasAlmeida-fx6eb
@LucasAlmeida-fx6eb 4 жыл бұрын
thanks :D quick and well explained!
@iftikharulmulk4405
@iftikharulmulk4405 3 жыл бұрын
Marvelous bro marvelous
@dodgersv3
@dodgersv3 5 жыл бұрын
You are awesome Mosh!!!
@petrospetrosyan1414
@petrospetrosyan1414 4 жыл бұрын
What's the difference between using “let” and “var”?
@djBulba
@djBulba 6 жыл бұрын
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?
@krishnamalik5563
@krishnamalik5563 3 жыл бұрын
Whats your color theme in VS Code ??
@siddharthashankarbarik6370
@siddharthashankarbarik6370 5 жыл бұрын
nice explanation ..
@naadir9680
@naadir9680 4 жыл бұрын
thanks a lot
@Steffi5376
@Steffi5376 3 жыл бұрын
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
@yekularajasekhar2206
@yekularajasekhar2206 4 жыл бұрын
Great video
@appstocks5956
@appstocks5956 Жыл бұрын
What theme is he using?
@sayantaniguha8519
@sayantaniguha8519 3 жыл бұрын
for(var i=1;i
@RanVargas
@RanVargas 5 жыл бұрын
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.
@shanmugapriya8562
@shanmugapriya8562 4 жыл бұрын
how to enable window and global object in node environment?
@abhishekshah11
@abhishekshah11 4 жыл бұрын
Where can I find more info about the function encapsulation? Now I'm curious
@lukehatcher98
@lukehatcher98 4 жыл бұрын
god tier explanation thank you, sm
@sarthakgaba1583
@sarthakgaba1583 4 жыл бұрын
Thank you!
@helloken
@helloken 4 жыл бұрын
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.
@milos5247
@milos5247 4 жыл бұрын
Hi, what color theme are you using for VSCode? Thanks
@code-island
@code-island 4 жыл бұрын
well explained
@sanjothomas3813
@sanjothomas3813 3 жыл бұрын
Const??
@gizmo928
@gizmo928 4 жыл бұрын
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?
@drcl7429
@drcl7429 4 жыл бұрын
Yes. Better Encapsulation.
@ytmrdk
@ytmrdk 4 жыл бұрын
THANKS!
@suhedaerturk215
@suhedaerturk215 4 жыл бұрын
Mosh is the best!!!
@1988naveensaini
@1988naveensaini Жыл бұрын
Love You bro
@urielhd1516
@urielhd1516 2 жыл бұрын
thx
@shashikumar-ut6uu
@shashikumar-ut6uu 6 жыл бұрын
Please put this course in udemy. I love your videos. You're the best
@muhdamin340
@muhdamin340 3 жыл бұрын
what is his apps ? what name of that apps? i
@DaRK-ym3ol
@DaRK-ym3ol 3 жыл бұрын
const is don't can change.
@konstantinstefanov365
@konstantinstefanov365 2 жыл бұрын
I think you forgot the constant from your title :)
@FilipPandrc
@FilipPandrc 2 жыл бұрын
1:39 "I becomes 5 because 5 is not less than 5" Can someone explain this?
@trueUtd
@trueUtd 2 жыл бұрын
See when for loop works It first initialises i=0 The condition is checked if i is less then 5, then we move inside code block and console.log works and only after that i++ works and i is increased to 1, So what you are asking is, when i reaches to i=4, the console.log works for the last time and then i becomes 5 and then when condition is checked it fails and we move out of the for loop. This is where var and let differ when we use var it allows i to be accessed globally and let only works within for loop hence let doesn't go to console.log which is outside for loop, But var does and hence 5 is also printed.
@moazelsawaf2000
@moazelsawaf2000 5 жыл бұрын
Thanks a lot
@BlazertronGames
@BlazertronGames 5 жыл бұрын
Weird. Game maker uses var for local variables, it's the opposite in js.
@mnageh-bo1mm
@mnageh-bo1mm 5 жыл бұрын
what if you just used test = 'color';
@prathyushsunny
@prathyushsunny 3 жыл бұрын
But why are there like, MOST of the developers that use var over let?
@foxjonesofficial
@foxjonesofficial 3 жыл бұрын
I still use var
JavaScript Cloning an Object
5:20
Programming with Mosh
Рет қаралды 84 М.
Differences Between Var, Let, and Const
8:37
Web Dev Simplified
Рет қаралды 186 М.
Stay on your way 🛤️✨
00:34
A4
Рет қаралды 31 МЛН
The Joker saves Harley Quinn from drowning!#joker  #shorts
00:34
Untitled Joker
Рет қаралды 54 МЛН
Идеально повторил? Хотите вторую часть?
00:13
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 17 МЛН
JavaScript var, let, and const explained
11:56
Kevin Powell
Рет қаралды 23 М.
JavaScript this Keyword
8:21
Programming with Mosh
Рет қаралды 453 М.
Object-oriented Programming in 7 minutes | Mosh
7:34
Programming with Mosh
Рет қаралды 3,9 МЛН
JavaScript ES6 Arrow Functions Tutorial
9:32
Web Dev Simplified
Рет қаралды 826 М.
The Complete Web Development Roadmap [2024]
15:15
Programming with Mosh
Рет қаралды 82 М.
JavaScript Scope (Local vs Global)
5:43
Programming with Mosh
Рет қаралды 56 М.
JavaScript Array Reduce
7:42
Programming with Mosh
Рет қаралды 335 М.
Learn JavaScript Hoisting In 5 Minutes
5:40
Web Dev Simplified
Рет қаралды 127 М.