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

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ก.ค. 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
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 67

  • @neatunet
    @neatunet ปีที่แล้ว +14

    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.

  • @someguy6076
    @someguy6076 5 วันที่ผ่านมา

    Thanks, Dave !!
    This one came via a link from my lecturer, but I have worked thru a number of your multi-hour videos previously. Your explanations have helped me to understand many of the core concepts of JavaScript programming and web development.

  • @farazk9729
    @farazk9729 2 ปีที่แล้ว +2

    Wow! What a thorough explanation. Amazing Dave; by far the best on TH-cam.

  • @bbalot2675
    @bbalot2675 2 ปีที่แล้ว

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

  • @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.

  • @user-og7qb1yf5l
    @user-og7qb1yf5l 5 หลายเดือนก่อน

    Roadmap redirected me to your channel, what a tutorial!

  • @anaswaras6305
    @anaswaras6305 2 ปีที่แล้ว

    Awesome tutorial..Thanks a bunch!

  • @FredRosa
    @FredRosa 3 ปีที่แล้ว +2

    Amazing explanation. Great work Dave. Thank you.

  • @movierecommendation6667
    @movierecommendation6667 ปีที่แล้ว

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

  • @mohammadahmedragab837
    @mohammadahmedragab837 ปีที่แล้ว

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

  • @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

  • @StellaOloo
    @StellaOloo ปีที่แล้ว

    This is so detailed and easy to understand.Thank you

  • @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

  • @NeoTheChoosenOne
    @NeoTheChoosenOne 3 ปีที่แล้ว

    Great explanation, thank you!!!

  • @yatin1370
    @yatin1370 ปีที่แล้ว

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

  • @jawidforotan3260
    @jawidforotan3260 3 ปีที่แล้ว +1

    Your videos are amazing !!!

  • @Monika-tx1ge
    @Monika-tx1ge 2 ปีที่แล้ว

    Great explanation!

  • @dr_corrupt1014
    @dr_corrupt1014 ปีที่แล้ว

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

  • @end-me-please
    @end-me-please ปีที่แล้ว

    You have a gift for teaching 💯

  • @kevinjohnson1769
    @kevinjohnson1769 ปีที่แล้ว

    Great explaination

  • @Chlorine-DB
    @Chlorine-DB 6 หลายเดือนก่อน

    This guy is the god of JS

  • @jamshidtashkent1976
    @jamshidtashkent1976 ปีที่แล้ว

    Perfect

  • @Grihlo
    @Grihlo ปีที่แล้ว

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

  • @tauhidhasan5527
    @tauhidhasan5527 ปีที่แล้ว

    Thanks I am now clear

  • @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.

  • @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 ``.

  • @ezozaezoza3472
    @ezozaezoza3472 ปีที่แล้ว

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

  • @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.

  • @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 💯

  • @yigita3190
    @yigita3190 3 ปีที่แล้ว +1

    Holy shit a youtube tutorial with a good microphone

  • @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?

  • @taetaebeatz5578
    @taetaebeatz5578 3 ปีที่แล้ว

    Never underestimate the basics...