Differences Between Var, Let, and Const

Sdílet
Vložit
  • čas přidán 9. 11. 2018
  • You have probably watched many different tutorials, and you may have noticed that some tutorials use var to declare variables while others use let or even const. It can get confusing quickly on what the differences are between the keywords, so in this video I am going to be explaining everything you need to know about var, let, and const. I will be going over their differences as well as explaining what situations you should use each keyword.
    If you have any suggestions for a JavaScript topic, please let me know in the comments below.
    Let vs Const vs Var Article:
    blog.webdevsimplified.com/202...
    Twitter:
    / devsimplified
    GitHub:
    github.com/WebDevSimplified
    CodePen:
    codepen.io/WebDevSimplified
    #VarVsLet #JavaScript #WebDevelopment

Komentáře • 206

  • @ianoliver6828
    @ianoliver6828 Před 3 lety +218

    You have a super rare skill. It's rare to find someone who is both a skilled programmer AND a skilled teacher / communicator. Thank you for your videos

  • @Max-nm8ct
    @Max-nm8ct Před 3 lety +40

    Mate, I must say, your hair is on fleek!

  • @camilafloressanhueza7966
    @camilafloressanhueza7966 Před 3 lety +152

    OMG!! I had seen 100000 videos whose explanations were extremely complicated, but you explained it in less than 5 minutes!! Thank you very much

  • @dozadetrezire7455
    @dozadetrezire7455 Před 2 lety +23

    You forgot to say one more thing: when you declare a variable with var keyowrd, this automatically creates a property inside the Window ojbject.. so, if you say for example: var x = 1; and after that you say: console.log(window)... you will see that in the window object you will find the property X with the value of 1. Instead, if you declare a variable with let or const, this won't ceate a property inside the Window object. I guess this was also very important to mention!

  • @jthomasaurus
    @jthomasaurus Před 4 lety +6

    Another super-clear explanation. Love your video style, brevity, and way of explaining everything. Plain and simple!

  • @AlexTechie
    @AlexTechie Před 5 lety +59

    6:36 I didn't know that. This is why I always watch your videos, even if I think I understand the subject before hand. Great work! 👍

    • @WebDevSimplified
      @WebDevSimplified  Před 5 lety +13

      It is a bit counter intuitive based on the name. You can use Object.freeze to create an object that cannot have the properties reassigned, but it will still let you reassign a nested property such as company.ceo.name = 'New name'

    • @AlexTechie
      @AlexTechie Před 5 lety +4

      @@WebDevSimplified thanks for the tip! As I was watching the part I referenced earlier, I was wondering how we would assign constants to the properties of an object, so it's like you read my mind. :D

  • @dorothyglade9087
    @dorothyglade9087 Před 2 lety +1

    This was great, you speak at just the right speed and your voice is clear. This really helped me and my husband with something in our bootcamp.

  • @TheSlimSh4dy
    @TheSlimSh4dy Před 3 lety +11

    Thank you for the clear explanation! Currently, I'm learning HTML, CSS & Javascript and if I don't understand something, I'm firstly looking at your channel to find an explanation. If it doesn't exist on your channel, then I'm searching in other sources. You are a very good teacher, your videos are short, consistent and very clear, without any unnecessary content. Thank you very much for sharing your time to make these videos.

  • @santra528
    @santra528 Před 4 lety

    This is complete explanation of let vs const vs var I have ever seen. Thanks Kyle 😊

  • @pratikthorat3480
    @pratikthorat3480 Před 2 lety +1

    This is the best explanation I've ever seen. I do know the differences yet I learnt something new

  • @raaghavendrahm4883
    @raaghavendrahm4883 Před 3 lety

    I am just starting with JS and I was stuck with this like hell. You made it so clear. Thanks a lot!!

  • @AidanJoyce
    @AidanJoyce Před rokem +2

    Would like to say, well done and thank you for a clear and concise explanation of the similarities, differences and when to use, Var, Const and Let.
    Watched other videos and without doubt, you nail it better here with both explanation and example.
    Will be watching more of your stuff as this was a great measure stick for the quality of teaching.
    Keep up the great work.

  • @youareuglybuti
    @youareuglybuti Před rokem

    Thank you man, was struggling to understand these basic concepts. You are a lifesaver.

  • @moonlightCR7
    @moonlightCR7 Před rokem

    Thanks a billion, Kyle for explaining the difference among these keywords.

  • @jorgefrias7889
    @jorgefrias7889 Před rokem

    Amazing explanation. Simple and straight to the point. Thank you!

  • @rohit9969171579
    @rohit9969171579 Před 4 lety +11

    It was very crystal clear explanation thank you.

  • @guillaumedupont2902
    @guillaumedupont2902 Před 3 lety

    This was SO helpful. Thank you so much!

  • @dacfniel5442
    @dacfniel5442 Před 4 lety

    You are the best teacher on CZcams.

  • @baliyachtservices7505
    @baliyachtservices7505 Před 2 lety

    I was wondering just that! Thanks so much

  • @pooja6380
    @pooja6380 Před rokem +1

    learn so much important points and the actual difference ....thanks a lot

  • @dsvhs93
    @dsvhs93 Před 3 lety +1

    Whenever Kyle comes out with the JavaScript class that he is working on.... I am purchasing it. He is awesome! Thank you Kyle!

  • @borislavaborisova7274

    Thank you! This video was very helpful. Just what I needed.

  • @ev_dash
    @ev_dash Před rokem

    Your videos are helping me out alot! Thank you ❤

  • @therealbrucewayne24
    @therealbrucewayne24 Před rokem +1

    Yes I did learn something thank you very much man it was hard for me to understand this but you explained it in like less than 10 minutes lol you have a good day too :)

  • @tonic.9234
    @tonic.9234 Před 2 lety +1

    I never knew var worked like this, and I've been using Javascript almost daily for over 2 years. I always assumed it worked like let because I have a background in other programming languages and assumed it's scoping worked the same. So wild! Thanks for the clarification!

  • @thecoderabbi
    @thecoderabbi Před 3 lety +1

    Thank you very much Kyle, This is great. Very explicit. I think I would begin using the 'const' instead of the 'var'
    Thanks ✌️

  • @user-se6ct5zu2f
    @user-se6ct5zu2f Před 2 lety

    huge thanks for the explanation, only you made it clear to me!

  • @nessparadis6948
    @nessparadis6948 Před rokem

    Great video. Succinct, clear to understand, and I learned a lot. Thank you.

  • @gqntexas
    @gqntexas Před 2 lety

    Great great explanation.. you always here that the differences is the 'block scope' and then wonder, well, like within an if or for loop, but nobody else ever says that...

  • @michaelmaloy6378
    @michaelmaloy6378 Před rokem +1

    Whenever I see that face, I know useful information is coming my way.
    Simple, clear, and very helpful. Thank you as always!

  • @oceanmind1793
    @oceanmind1793 Před 2 lety

    You made it so clear. Thanks a lot

  • @bujjivenkateshwarlu6942

    Hey thanks. earlier i had confusion about block scope and Function scope. Now its cleared.

  • @wr1818
    @wr1818 Před 2 lety

    thankyou for all the amazing explaining!

  • @sadhappy8860
    @sadhappy8860 Před 8 měsíci

    Super clear explanation, many thanks

  • @JEffinger
    @JEffinger Před rokem

    Thank's bro that was a really clear explanation.

  • @sagarioharvey6910
    @sagarioharvey6910 Před rokem

    Web dev here in the PH. "Man I love your videos, your making a great impact" //awesome

  • @aisana99
    @aisana99 Před rokem

    thank you :D you shared more differences than my constructor in my Full-Stack Bootcamp! Will subscribe and watch all other videos of yours

  • @JonasGavenas
    @JonasGavenas Před rokem

    Great lesson!
    Thank you, perfect explain.

  • @injector6926
    @injector6926 Před 2 lety

    Best Web Development I seen evrr

  • @tiltify2393
    @tiltify2393 Před 2 lety

    finally I understand the difference, thanks man A LOT

  • @derickbank1064
    @derickbank1064 Před 2 lety

    Thank you for the explanation. Well explained. !

  • @videospromoter5884
    @videospromoter5884 Před 4 lety

    Checked 10s of video and this video told me hmm you understand this now . say thanks to him :) Thanks Dude

  • @B3ASTM0DE1
    @B3ASTM0DE1 Před 5 lety +8

    This video came just in time. We are going through Javascript and JQuery right now in my bootcamp!

    • @WebDevSimplified
      @WebDevSimplified  Před 5 lety +3

      I'm really glad to hear. If you run into anything that confuses you in the bootcamp feel free to let me know, and I can try to help or make a video on the topic.

    • @B3ASTM0DE1
      @B3ASTM0DE1 Před 5 lety

      @@WebDevSimplified Awesome will do!

  • @weligamage1
    @weligamage1 Před 2 lety

    Clear and concise, Thank you

  • @hoakeen
    @hoakeen Před 2 lety

    Awesome tutorials! Thank you!

  • @kazeemalatishe3828
    @kazeemalatishe3828 Před 2 lety

    Thanks a lot. You explain things really well.

  • @sandybathwater8385
    @sandybathwater8385 Před 2 lety

    So simple, thank you so much! If I search for something and see your face in the results, I am saved. :)

  • @mirzashowvik564
    @mirzashowvik564 Před rokem

    Great video !!!
    Love it

  • @khisus20
    @khisus20 Před 2 lety

    This is truly simplified

  • @shantanukulkarni8883
    @shantanukulkarni8883 Před rokem +5

    Basically, _let_ is _var_ done right.

  • @monireachtang712
    @monireachtang712 Před 3 lety

    very clear explanation! you are awesome!

  • @nobitalazy9791
    @nobitalazy9791 Před 2 lety

    you are a rare gem my friend!!!

  • @FredRosa
    @FredRosa Před 3 lety

    Great tutorial, thanks!

  • @somtovitus
    @somtovitus Před 3 lety

    Best video on this topic

  • @jimilittle
    @jimilittle Před 2 lety

    That was awesome. Thank you

  • @user-yi4zd6gh5h
    @user-yi4zd6gh5h Před rokem

    great explanation. TY!

  • @hanntongfong1226
    @hanntongfong1226 Před 3 lety

    Great work! 👍

  • @ihaseeb_khan
    @ihaseeb_khan Před 4 lety

    Thank you bro, really helpful :)

  • @shresthsrivastava27
    @shresthsrivastava27 Před 3 lety

    Awesome explanation !

  • @pashashyba5006
    @pashashyba5006 Před 2 lety

    Quite easy to understand. Thanks

  • @derrick3534
    @derrick3534 Před 5 lety +5

    man you are teaching the things that are not widely discussed which is why i am going to subscribe. You have a true gift of teaching in simple terms. i enjoy your channel, especially the tutorial on promises

    • @WebDevSimplified
      @WebDevSimplified  Před 5 lety +2

      Thank you! I try to cover the topics that I know confused me or other people I know, because those will be the most helpful for others.

  • @RS-nz6rn
    @RS-nz6rn Před 3 lety

    Thanks for clarifying.

  • @tinpham6413
    @tinpham6413 Před 3 lety

    Love your channel

  • @andreastewart6133
    @andreastewart6133 Před 2 lety

    Helpful. Thank you.

  • @priyanshugupta7614
    @priyanshugupta7614 Před rokem

    thank you your explanation was very clear

  • @coderbhai4832
    @coderbhai4832 Před 2 lety +23

    1)Declaration Let variable in a {} block scope can't be access outside of it
    2)let name = value1, let name = value2, can't redeclare let twice
    Instead of this should be written name=value2;
    3)let can be change it’s value const can't as it is constant but it can change its object value.🙃

    • @chalk1007
      @chalk1007 Před 2 lety

      yeah, I was a little confused aswell but this video was made in 2018

    • @dozadetrezire7455
      @dozadetrezire7455 Před 2 lety +1

      You forgot to say one more thing: when you declare a variable with var keyowrd, this automatically creates a property inside the Window ojbject.. so, if you say for example: var x = 1; and after that you say: console.log(window)... you will see that in the window object you will find the property X with the value of 1. Instead, if you declare a variable with let or const, this won't ceate a property inside the Window object. I guess this was also very important to mention!

  • @theNikijspictures
    @theNikijspictures Před rokem

    Really good explanation, for beginner level - if reading info only in text (with no visual examples), it gets confusing quickly on this topic. Cheers😉

  • @mayorb37
    @mayorb37 Před rokem

    You make a wonderful work 👍👍👍👍👍 thanks

  • @53strat55
    @53strat55 Před 3 lety

    Solid video!

  • @tomschwrs
    @tomschwrs Před rokem

    Great Video!

  • @sebnargeurbrok1374
    @sebnargeurbrok1374 Před 3 lety

    well explained, thanks!

  • @aadityakiran_s
    @aadityakiran_s Před rokem

    I came from C#. It's pretty simple there. Use var for what you wanna change, const for what you don't wanna change; variable and constant. Simple language for simple people.

  • @jankiramulu
    @jankiramulu Před rokem

    Wonderful explanation.. Than you....

  • @robertoriosmorales7528

    awesome explanation

  • @andyarellano7474
    @andyarellano7474 Před 2 lety

    Thank you so much bro.

  • @letsgetto1millwithoutvids

    Good video finally understand and I could have used var for something I needed instead of saving it to local storage and grabbing it after 🤦‍♀️

  • @placeholder_name321
    @placeholder_name321 Před 3 lety

    so so simple. Finally.

  • @vikasnigam9646
    @vikasnigam9646 Před 2 lety

    Gr8 explanation.

  • @EyeIn_The_Sky
    @EyeIn_The_Sky Před 3 lety +2

    Good old Java script, Let (no pun intended) us introduce "const" that does not allow for re-assigning to avoid human error so that it is "constant" except when it's not...

  • @kamalsyed4237
    @kamalsyed4237 Před 4 lety

    loved it! Thanks.. Const, Let, Var .. it is :)

  • @redpadweb
    @redpadweb Před 2 lety

    Thank you so much.

  • @agaming7888
    @agaming7888 Před rokem

    thanks for this now i know what to do 👍👍👍👍

  • @johanledel1407
    @johanledel1407 Před 4 lety +1

    Thank you!

  • @halofreak644
    @halofreak644 Před 3 lety +1

    You the real MVP

  • @stuartgreen5217
    @stuartgreen5217 Před 3 lety

    Been building sites for 30 years and always avoided JS due to pre jquery experience. Am now taking the time to relearn JS and node while I’m at it and I have to say I’m regretting not doing this sooner. I’ve been programming in C# php Perl VB and JS is a peace of cake by comparison .

  • @vikumravinayake1882
    @vikumravinayake1882 Před 3 lety

    Problem solved
    Thank you very much. Ur a good explainer....

  • @jahneychriast2141
    @jahneychriast2141 Před 4 lety +2

    தெளிவான விழக்கம் நன்றி 🙏🏽

  • @ruslankovalov8292
    @ruslankovalov8292 Před 3 lety

    Thanks God we have you to explain this!! thanks!! A lot! A mean that)

  • @NehaGupta-fd8hs
    @NehaGupta-fd8hs Před 4 lety

    Great Stuff!

  • @jousufjacob8251
    @jousufjacob8251 Před 2 lety

    you are the man ,!

  • @kushagraraj3163
    @kushagraraj3163 Před rokem

    you r jordi of webdev !!!!!!!

  • @themixmaster
    @themixmaster Před 2 lety

    thanks a lot man

  • @langgs
    @langgs Před 4 měsíci

    THNX!!!

  • @surendergaini3365
    @surendergaini3365 Před rokem

    GOOD explaintion

  • @elkootje
    @elkootje Před 2 lety

    Thank you.

  • @rickneibauer1
    @rickneibauer1 Před 2 lety

    I can't move on until i know why something works unless i ABSOLUTELY have to. thanks to you i can move toward the light. goodbye everyone...

  • @NedumEze
    @NedumEze Před 3 lety +1

    I love your approach so much. Thank you.
    I do have a fundamental problem though. Maybe you can consider making video of it. It is this:
    When I create an external JS file, and I have in it, codes like functions, etc in it, only the first code or function works!
    What should I do to ensure that different codes intended to fire at different Listeners, in the same file, work?

  • @RMNSH99
    @RMNSH99 Před 4 lety

    Thanks Kyle

  • @itsshafayat6378
    @itsshafayat6378 Před měsícem

    someone get this absolute master a stellar coding setup i.e a better keyboard

  • @khaktus
    @khaktus Před 2 lety +1

    While I prefer Kyle's tutorials over the others for various reasons (extent > clarity > simplicity), for this one I'll differ from the praise-storm below - this was just one of the many confusing explanation of the var|let|const topic that I've seen so far.
    This is the problem that many skilled developers have - they assume that as developers they have analytical thinking and provide well structured explanations (naturally, by default) - but I have seen - since the university times to professional days - that the paradoxical situation happens: techies often do the exact opposite: they do not adhere to the structure/hierarchy of the topic, nor the consistent use of terms they set in the beginning. :D Something to work on throughout professional life.
    In first part, you repeatedly state that var does and let does not allow to redeclare the variable, then you say the difference between let and const is that const does not allow to redeclare the variable. That paused my brain, while explanations went on. Maybe stating more clearly what is the difference between (your use of) "redeclare" and "redefine" and "reset" and "reassign" ... would help. Particularly in this one topic. :)
    I'd welcome more "technical" "analytical" (or tabular ;) ) than the "narrative" approach here. And particularly a consistent use of terms.
    1 minute:
    redeclare:
    var|let|const myThingy = 10
    var|let|const myThingy = 20
    redefine/reassign/reset variable value:
    var|let|const myThingy = 10
    myThingy=20
    redefine object|array items value (not object|array value, which is just its memory address, so that remains "constant"):
    var|let|const myObject = [10, 11];
    myObject[0] = 20
    1 minute:
    | redeclare| reassign value of variable | reassign value of item in the array or property in the object|
    ------------------------------------------------------------------------------------------------------------------------------------------------------------
    var yes yes yes
    let no yes yes
    const no no yes
    Also, from the many lessons on the scope, I understood that "scope" is something defining on what level of nesting we are (regardless if it is a function or block or just anything between {}). This perspective was often emphasized. Here, for the first time, I hear that different scope is actually defined by either function or block. Good point here!

    • @nowhererealm
      @nowhererealm Před rokem

      dude yes! I was wondering how the heck are there so many people praising the explanation when i couldnt understand why he said let cant redefine a variable then goes on and says thats the only difference between const and let?? lmao wtf