JavaScript Let vs Var vs Constant | Mosh

SdĂ­let
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

Komentáƙe • 187

  • @sanket_sy
    @sanket_sy Pƙed 4 lety +186

    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 Pƙed 3 lety

      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 Pƙed 3 lety

      @@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 Pƙed 3 lety +2

      also, const is also block-scoped like "
      let"

    • @deepakkumaryadav6009
      @deepakkumaryadav6009 Pƙed 2 lety

      @@Cool94_4u
      you are bigineer so why you are doing finger bro in hot burner .

    • @harshagarwal00
      @harshagarwal00 Pƙed rokem +1

      @@Cool94_4u node uses "global" instead of "window" object

  • @m.u.550
    @m.u.550 Pƙed 6 lety +94

    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 Pƙed 5 lety

      I cannot agree more with you! Mosh is the best!

    • @yutaitadori7318
      @yutaitadori7318 Pƙed 2 lety

      @@programmingwithmosh I have seen your full sql course

    • @yutaitadori7318
      @yutaitadori7318 Pƙed 2 lety

      It's just awesome

    • @plasmaastronaut
      @plasmaastronaut Pƙed 11 měsĂ­ci

      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 Pƙed 6 lety +31

    Finally, I understood the difference between var and let. Great video.

  • @codinginflow
    @codinginflow Pƙed 2 lety +6

    Short and to the point explanation! Thank you very much! I'm learning JS right now 😁

  • @memaimu
    @memaimu Pƙed 4 lety +106

    I came here to learn about const. I left without that knowledge.

    • @crepkey
      @crepkey Pƙed 4 lety +17

      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 Pƙed 4 lety

      Go back to it and Google all the terminology in videos that will make a great difference sir.

    • @charankumar9897
      @charankumar9897 Pƙed 4 lety +1

      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 Pƙed 3 lety +1

      Late, but const is just let but you can’t change the value afterwards.

    • @Victory_Accomplished
      @Victory_Accomplished Pƙed 3 lety

      The point is this video is talking about let and var not const

  • @pranjalijambhule6729
    @pranjalijambhule6729 Pƙed 4 lety +1

    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 Pƙed 5 lety +1

    The best explanation of let and var keyword so far, Thanks

  • @ga7853
    @ga7853 Pƙed 5 lety

    Thank you so much, that was crystal clear.

  • @kicn
    @kicn Pƙed 4 lety

    What a wonderful way to explain. loved it.

  • @blub579
    @blub579 Pƙed 2 lety

    Packed with information, but short and concise. great video

  • @frankvee
    @frankvee Pƙed 3 lety +8

    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.

  • @dijiflex
    @dijiflex Pƙed 4 lety

    Clear and straight to the point

  • @Shiva-zy7jq
    @Shiva-zy7jq Pƙed 4 lety

    Thanks for this video. Finally understood the difference between var and let

  • @evans_2103
    @evans_2103 Pƙed 2 lety

    The best explanation ever! Thank you very much!

  • @kevinpatel5106
    @kevinpatel5106 Pƙed 4 lety

    first time watching your video, great explanation! Thank you for your help!

  • @maulikgevariya3059
    @maulikgevariya3059 Pƙed 2 lety

    Finally I understood the differance thanks Mosh !

  • @sumonchandrashil8406
    @sumonchandrashil8406 Pƙed 4 lety

    Awesome. I understood very well.

  • @samithakulatilaka
    @samithakulatilaka Pƙed 4 měsĂ­ci

    A great and simple explanation of var vs. let. Thank you Mosh.

  • @tochachochi
    @tochachochi Pƙed 5 lety

    Great content loko. I subscribed by the 3rd video.

  • @techlightdev
    @techlightdev Pƙed rokem

    Great tutorial, I've more understand with it. Thank you

  • @LucasAlmeida-fx6eb
    @LucasAlmeida-fx6eb Pƙed 4 lety

    thanks :D quick and well explained!

  • @jerryrobinson958
    @jerryrobinson958 Pƙed 3 lety

    This video was well done. Mosh, you tha man

  • @milindkhadse556
    @milindkhadse556 Pƙed 4 měsĂ­ci

    helpful information thank you

  • @naveedurrehman2931
    @naveedurrehman2931 Pƙed 3 lety

    love it SIr thank you

  • @theweb_developer
    @theweb_developer Pƙed rokem

    Super helpful you explained it very well.

  • @dodgersv3
    @dodgersv3 Pƙed 5 lety

    You are awesome Mosh!!!

  • @mikepalazzo
    @mikepalazzo Pƙed 5 lety

    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?

  • @berniecat8756
    @berniecat8756 Pƙed 5 lety +6

    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 Pƙed rokem

      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".

  • @RezaMohseni
    @RezaMohseni Pƙed 5 lety +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 :)

  • @rupamsutar9299
    @rupamsutar9299 Pƙed rokem

    This was an indepth ans to the question.
    Thanks Mosh ❀❀

  • @znoran
    @znoran Pƙed 2 lety

    Very good and clear explaination my friend!!

  • @driassasofiane2839
    @driassasofiane2839 Pƙed rokem

    i was not good at all in the variables but now im proud of myself thank MR mosh

  • @gautambedi591
    @gautambedi591 Pƙed rokem

    Mosh, You are the best man

  • @human151
    @human151 Pƙed 6 lety +13

    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.

  • @TheRonron1994
    @TheRonron1994 Pƙed 4 lety +1

    What's the concern with var if you really need it althrough out the function scope?

  • @petrchutny
    @petrchutny Pƙed 4 lety

    Thanks Mosh!

  • @VOGTLANDOUTDOORS
    @VOGTLANDOUTDOORS Pƙed rokem

    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 Pƙed rokem

      Waste of time typing this long😅

  • @iftikharulmulk4405
    @iftikharulmulk4405 Pƙed 3 lety

    Marvelous bro marvelous

  • @lukehatcher98
    @lukehatcher98 Pƙed 4 lety

    god tier explanation thank you, sm

  • @sarthakgaba1583
    @sarthakgaba1583 Pƙed 4 lety

    Thank you!

  • @milos5247
    @milos5247 Pƙed 4 lety

    Hi, what color theme are you using for VSCode? Thanks

  • @priyamganguly
    @priyamganguly Pƙed 3 lety

    The reference point to the Window object is quite significant. It may cause leaks and overrides. Good thing to know.

  • @naadir9680
    @naadir9680 Pƙed 4 lety

    thanks a lot

  • @abhishekshah11
    @abhishekshah11 Pƙed 4 lety

    Where can I find more info about the function encapsulation? Now I'm curious

  • @janus7156
    @janus7156 Pƙed 5 lety +130

    You didn't explain what const does.

    • @skalippanbalippan6972
      @skalippanbalippan6972 Pƙed 4 lety +3

      same as let => block scoped

    • @insideTheMirror_
      @insideTheMirror_ Pƙed 4 lety +16

      @@skalippanbalippan6972 nop. const is a constant, it's not a variable.

    • @philheathslegalteam
      @philheathslegalteam Pƙed 4 lety +11

      @@insideTheMirror_
      It is a variable. With immutable binding not value.

    • @insideTheMirror_
      @insideTheMirror_ Pƙed 4 lety +2

      @@philheathslegalteam Well I am not God here. Unlike you ;)

    • @crepkey
      @crepkey Pƙed 4 lety +13

      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.

  • @rolypolyyy
    @rolypolyyy Pƙed rokem

    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?

  • @pouyajabbarisani
    @pouyajabbarisani Pƙed 4 lety +1

    Hi Mosh,
    what is the name of your VSCode theme?

  • @hollenjf
    @hollenjf Pƙed 4 lety

    Global and private initialization. Good stuff

  • @ytmrdk
    @ytmrdk Pƙed 4 lety

    THANKS!

  • @yekularajasekhar2206
    @yekularajasekhar2206 Pƙed 3 lety

    Great video

  • @siddharthashankarbarik6370

    nice explanation ..

  • @tracetv8115
    @tracetv8115 Pƙed 3 lety

    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

  • @moazelsawaf2000
    @moazelsawaf2000 Pƙed 4 lety

    Thanks a lot

  • @wotanman7711
    @wotanman7711 Pƙed 4 lety

    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?

  • @MrRossss1
    @MrRossss1 Pƙed 5 lety

    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

  • @SimarpreetKaur-er1dw
    @SimarpreetKaur-er1dw Pƙed 2 lety

    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.

  • @braco0000
    @braco0000 Pƙed 3 lety

    Seeing the examples it's all clear... We must NEVER use var. Amazing how easy you make it look

  • @prashanttiwari120
    @prashanttiwari120 Pƙed 3 lety

    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 :)

  • @djBulba
    @djBulba Pƙed 6 lety

    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?

  • @Markkkkkkkkkkkkkkkkk
    @Markkkkkkkkkkkkkkkkk Pƙed 4 lety +2

    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

  • @RanVargas
    @RanVargas Pƙed 5 lety

    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.

  • @eduardohernandez552
    @eduardohernandez552 Pƙed 6 lety +1

    Really good tutorials!! You should make some courses on udemy! Thank you very much

  • @OfoeNelson
    @OfoeNelson Pƙed 3 lety

    Great video but it would have been nice to have it numbered since it appears to be part of a series.

  • @petersuba1125
    @petersuba1125 Pƙed 2 lety

    what if i want to declarate global variable inside of function?

  • @code-island
    @code-island Pƙed 4 lety

    well explained

  • @rohitkumarshrivastava9693

    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?

  • @1988naveensaini
    @1988naveensaini Pƙed rokem

    Love You bro

  • @pranavpatil5849
    @pranavpatil5849 Pƙed 4 lety +1

    Sir can you please make a video on Closures in JS..

  • @SamuelHauptmannvanDam
    @SamuelHauptmannvanDam Pƙed 3 lety

    So for, for loops, I should still use var?

  • @krishnamalik5563
    @krishnamalik5563 Pƙed 3 lety

    Whats your color theme in VS Code ??

  • @suhedaerturk215
    @suhedaerturk215 Pƙed 4 lety

    Mosh is the best!!!

  • @xxsaifxx2450
    @xxsaifxx2450 Pƙed 4 lety

    so should we use them in some cases or avoid using them forever

  • @mohitnegi724
    @mohitnegi724 Pƙed 5 lety

    Finally. I got it.

  • @iq0578
    @iq0578 Pƙed 3 lety +1

    Very clear explanation, but I don't understand the why var is a problem at 3:13

  • @cicerofonseca8604
    @cicerofonseca8604 Pƙed 5 lety

    Awesome...you explained it so simple. Finally, I got the difference!

  • @shanmugapriya8562
    @shanmugapriya8562 Pƙed 4 lety

    how to enable window and global object in node environment?

  • @urielhd1516
    @urielhd1516 Pƙed 2 lety

    thx

  • @yuriaugusto1084
    @yuriaugusto1084 Pƙed 2 lety

    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;

  • @appstocks5956
    @appstocks5956 Pƙed rokem

    What theme is he using?

  • @Levelonesucks
    @Levelonesucks Pƙed 4 lety

    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.

  • @mudasirh1404
    @mudasirh1404 Pƙed 3 lety

    const is also block-scoped, but It can't be reassigned. You can modify them but can't reassign them.

  • @equality9304
    @equality9304 Pƙed 5 lety

    10/10 your performance

  • @kondasomu
    @kondasomu Pƙed rokem

    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.

  • @yamaikaguya5550
    @yamaikaguya5550 Pƙed 4 měsĂ­ci

    its uniqueee

  • @manuelcasares7270
    @manuelcasares7270 Pƙed 3 lety

    For some reason
    var apple = 'apple';
    console.log(window.apple)
    throws undefined, like It is not attaching apple to the window object..

  • @gizmo928
    @gizmo928 Pƙed 4 lety

    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 Pƙed 4 lety

      Yes. Better Encapsulation.

  • @Brian-bo2fu
    @Brian-bo2fu Pƙed 5 lety +2

    The question is where do those variables declared using let or const go?? Somewhere in the memory?

    • @speedysense5061
      @speedysense5061 Pƙed 5 lety +1

      Yes, JavaScript are stored in two places: stack (local context) and heap (store dynamically).

    • @speedysense5061
      @speedysense5061 Pƙed 5 lety

      You can subscribe our new post or follow us on speedysense.com/var-vs-let-vs-const-in-javascript/

  • @muhdamin340
    @muhdamin340 Pƙed 3 lety

    what is his apps ? what name of that apps? i

  • @Steffi5376
    @Steffi5376 Pƙed 3 lety

    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

  • @chandankumarthakur7182
    @chandankumarthakur7182 Pƙed 5 lety +4

    let sayHi =function() { console.log('hi');}

    • @Himanshu_Sharma..
      @Himanshu_Sharma.. Pƙed 4 lety

      Nice one my friend 👍👊👌 I understood what you were communicating

  • @petrospetrosyan1414
    @petrospetrosyan1414 Pƙed 4 lety +1

    What's the difference between using “let” and “var”?

  • @bigbadcatbigbcy2933
    @bigbadcatbigbcy2933 Pƙed 5 měsĂ­ci

    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

  • @gaminginn6072
    @gaminginn6072 Pƙed 3 lety

    why we even have var then? What's their purpose/adventage of it if any

  • @shashikumar-ut6uu
    @shashikumar-ut6uu Pƙed 6 lety +1

    Please put this course in udemy. I love your videos. You're the best

  • @virCottoQ37
    @virCottoQ37 Pƙed 4 lety

    no const :(
    but this video helped me little bit, so thanks anyway :)

  • @mounirkanane8083
    @mounirkanane8083 Pƙed 2 lety

    What about constant?

  • @Icecodes
    @Icecodes Pƙed 4 lety

    New access modifiers are succeeding Javascript

  • @konstantinstefanov365
    @konstantinstefanov365 Pƙed 2 lety

    I think you forgot the constant from your title :)

  • @foxjonesofficial
    @foxjonesofficial Pƙed 3 lety +1

    I still use var

  • @mnageh-bo1mm
    @mnageh-bo1mm Pƙed 5 lety

    what if you just used
    test = 'color';