var let const in Javascript Scope | Understanding Global Local Function Block Scope

Поделиться
HTML-код
  • Опубликовано: 13 июл 2024
  • Web Dev Roadmap for Beginners (Free!): bit.ly/DaveGrayWebDevRoadmap
    In this video we'll compare the keywords var, let, and const in JavaScript which also requires an understanding of JavaScript scopes such as global scope, local scope, function scope, and block scope.
    ▶ This video is part of the JavaScript Tutorials for Beginners Playlist found here: • Javascript Tutorials f...
    ✅ Quick Concepts outline:
    var, let, and const in Javascript
    (00:00) Intro
    (0:30) What makes var different
    (1:40) What makes let different
    (2:05) What makes const different
    (2:35) A discussion of scope
    (2:50) Global Scope
    (3:25) Local Scope
    (3:35) Block Scope
    (4:00) Function Scope
    (4:45) Local Scope variables declared with let or const are not available in global scope
    (6:00) Nested block scope inside of a function
    (7:05) Global scope variables are available in local scopes
    (8:00) Global scope is available to all scopes
    (8:25) If statements have block scopes
    (8:35) for loops have block scopes
    (8:45) switch statements have block scopes
    (9:00) Review of scopes so far...
    (9:50) A 2nd Example of global, function, and block scopes
    (14:40) Note: var is function scoped; const & let are block scoped
    (15:45) Review with helpful graphic :)
    📚 Further Reading:
    MDN Web Docs:
    Scope: developer.mozilla.org/en-US/d...
    Global Scope: developer.mozilla.org/en-US/d...
    Local Scope: developer.mozilla.org/en-US/d...
    Eloquent JavaScript Chapter 3:
    eloquentjavascript.net/03_fun...
    📺 More Beginner JS Videos:
    freeCodeCamp: • Learn JavaScript - Ful...
    Traversy Media: • JavaScript Crash Cours...
    The Net Ninja: • Modern JavaScript Tuto...
    ✅ Follow Me:
    Twitter: / yesdavidgray
    Reddit: / daveoneleven
    Medium: / davegray_86804
    #var #let #const
  • НаукаНаука

Комментарии • 66

  • @neatunet
    @neatunet Год назад +12

    Very detailed perfect lessons you have on your channel! I wish I found your channel earlier because not so many people explain basic foundational things so well. Good work :)

  • @CondeAlberto
    @CondeAlberto 2 года назад +3

    The best explanation of scope I have seen so far.
    It could have avoided me all those "undefined" errors a few weeks ago.
    Very good.

  • @viewsbyblur
    @viewsbyblur Год назад +1

    Ive been having trouble with scope for the absolute LONGEST, everything else in javascript has made sense except that. The second example at 9:50 was spot on.

  • @farazk9729
    @farazk9729 2 года назад +2

    Wow! What a thorough explanation. Amazing Dave; by far the best on RUclips.

  • @user-og7qb1yf5l
    @user-og7qb1yf5l 5 месяцев назад

    Roadmap redirected me to your channel, what a tutorial!

  • @bbalot2675
    @bbalot2675 2 года назад

    Thanks Dave! Been looking at several videos on scopes by far the best.

  • @bekhruzilhomov2599
    @bekhruzilhomov2599 10 месяцев назад +1

    Very understandable thanks a lot I liked your video thanks for creating this videos

  • @khairulislam-lp1iw
    @khairulislam-lp1iw 5 месяцев назад

    wow, loved it

  • @anaswaras6305
    @anaswaras6305 2 года назад

    Awesome tutorial..Thanks a bunch!

  • @movierecommendation6667
    @movierecommendation6667 Год назад

    Sometimes, all you need is a video and it does the work of a whole pdf. Thanks man.

  • @FredRosa
    @FredRosa 3 года назад +2

    Amazing explanation. Great work Dave. Thank you.

  • @mohammadahmedragab837
    @mohammadahmedragab837 Год назад

    thank you so much, your clear explanation made me understand the difference between var , let and cons. :)

  • @jamshidtashkent1976
    @jamshidtashkent1976 Год назад

    Perfect

  • @StellaOloo
    @StellaOloo Год назад

    This is so detailed and easy to understand.Thank you

  • @Chlorine-DB
    @Chlorine-DB 5 месяцев назад

    This guy is the god of JS

  • @VladimirJovana
    @VladimirJovana Год назад

    Can't recommend enough your channel, well thought explanations, concise, thanks a lot for sharing

  • @RahulSharma-oe6fy
    @RahulSharma-oe6fy 2 года назад

    Thank you for explaining the concept of variable leaking ! Never knew this happens with var

  • @dr_corrupt1014
    @dr_corrupt1014 Год назад

    this is the best explanation on scope I have ever seen 🎉

  • @yatin1370
    @yatin1370 Год назад

    Wonderfully compiled and delivered. Thanks for giving such crystal clarity in these fundamentals..

  • @Monika-tx1ge
    @Monika-tx1ge 2 года назад

    Great explanation!

  • @end-me-please
    @end-me-please Год назад

    You have a gift for teaching 💯

  • @jawidforotan3260
    @jawidforotan3260 3 года назад +1

    Your videos are amazing !!!

  • @NeoTheChoosenOne
    @NeoTheChoosenOne 3 года назад

    Great explanation, thank you!!!

  • @kevinjohnson1769
    @kevinjohnson1769 Год назад

    Great explaination

  • @tauhidhasan5527
    @tauhidhasan5527 Год назад

    Thanks I am now clear

  • @Grihlo
    @Grihlo Год назад

    It might be considered as not the easiest topic but with your explanation it's clear! Thank you!

  • @taetaebeatz5578
    @taetaebeatz5578 3 года назад

    Dang...I thought I knew this stuff...lol...good content...I learned a lot

  • @tymo3391
    @tymo3391 2 года назад +1

    Thank you, Dave! At 10:45 you marked three words "global" which are on seperate lines, and changed them simultaneously. How did you do that?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 года назад +2

      Highlight one of them and press Ctrl+D to select another. This is with VS Code on Windows.

  • @ukaszpospiech3817
    @ukaszpospiech3817 2 года назад +1

    Var doesn't respect { } scope indeed! Thanks for pointing this out. And thanks for explaining that the notorious "block scope" simply means things like: if { } or for { } etc.

  • @ezozaezoza3472
    @ezozaezoza3472 Год назад

    Мне очень жаль, что мало понимаю английский язык. Но я все равно понимаю эти уроки потому что вы объясняете очень хорошо и детально огромная вам благодарность

  • @yigita3190
    @yigita3190 3 года назад +1

    Holy shit a youtube tutorial with a good microphone

  • @chanalan5846
    @chanalan5846 2 года назад

    Hi Dave, thanks for the teaching.
    I've got a question, when I enter ${x}; in the console it shows ${x} instead of the value.
    May I know if there is anyway to solve this? Thanks

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 года назад

      ${x} will cause an error if that's all you type in the console. Unexpected token. Not sure why you would do that? If you have defined x as a variable, just type x.

    • @Grihlo
      @Grihlo Год назад

      Hi! I've had a similar issue recently and it was caused by using quotes (no matter singular or double '/") instead of backticks ``.

  • @damo190
    @damo190 2 года назад

    Sir, there is also a module scope. I saw in dmitripavlutin article that module scope can also be nested. How can I able to nest a module scope?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 года назад +1

      You import modules. They do have their own scope. This should be covered in a future lesson. 💯

    • @damo190
      @damo190 2 года назад

      @@DaveGrayTeachesCode so, importing a module is called module scope nesting?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 года назад

      @@damo190 no, modules have scope. Functions can be within a module, too. Their scope would be inside (aka nested) the module. If you understand the scope discussion of this tutorial, it is all you need as a beginner 💯

  • @taetaebeatz5578
    @taetaebeatz5578 3 года назад

    Never underestimate the basics...

  • @damo190
    @damo190 2 года назад

    let a = {f(){console.log(this);}};
    Here, the value of 'this' depends from where the function f is called, not from where f is defined. So, I doubt 'this' is in dynamic scope. But I have already learned, JavaScript only supports lexical(static) scoping. That's why I am confused. Please help me.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 года назад +1

      In your example, "this" refers to the object you are creating. You will see this more clearly if you add a 2nd property like num: 10 or similar and then call the f method on the object a like: a.f() ..it will log the full object to the console. The "this" keyword can make things confusing in JS until you work with it for awhile.

    • @damo190
      @damo190 2 года назад

      @@DaveGrayTeachesCode so, this keyword is in which scope, lexical or dynamic?