16.1: let vs var – Topics of JavaScript/ES6




In this video, I cover the “let” keyword in ES6 JavaScript. I discuss the differences between “let” and “var” (block scope vs. functional scope).
Video on “const”: https://youtu.be/2iLVFyYwyRA

Support this channel on Patreon: https://patreon.com/codingtrain
To buy Coding Train merchandise: https://www.designbyhumans.com/shop/codingtrain/
To Support the Processing Foundation: https://processingfoundation.org/support

Send me your questions and coding challenges!: https://github.com/CodingTrain/Rainbow-Topics

Contact:
Twitter: https://twitter.com/shiffman
The Coding Train website: http://thecodingtrain.com/

Source Code for the all Video Lessons: https://github.com/CodingTrain/Rainbow-Code

p5.js: https://p5js.org/
Processing: https://processing.org

For an Intro to Programming using p5.js: https://www.youtube.com/playlist?list=PLRqwX-V7Uu6Zy51Q-x9tMWIv9cueOFTFA
For Coding Challenges: https://www.youtube.com/playlist?list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH

Help us caption & translate this video!

https://amara.org/v/dXl2/

πŸ“„ Code of Conduct: https://github.com/CodingTrain/Code-of-Conduct

Original source


23 responses to “16.1: let vs var – Topics of JavaScript/ES6”

  1. This might sound like an odd question, but why can't I find this playlist on the Coding Train main YouTube site? In fact, there are a few tutorial series that I can't seem to directly, including the p5 Sound one. Am I missing something?

  2. This is really interesting. I had thought that it was some kind of optimization around declaring constants and variables that could change as functional programming became more fashionable. Thanks.

  3. You keep saying it will cause a problem, but what's the "problem"?

    I still have not to get into any problem by using var.
    I even prefer the way it hoisted the variable so that when I write a single file js with a lot of Functions; all the associated global variables can sit on top of it, no need for pages of global variable declaration on top of the file.
    I only declare i once and have no trouble with `for (i = 0)` without var/let.
    The way you can get the value of i outside of for loop is a plus for me (you can break and check the count by i)

    Anyway, My point is: You can't just say "It will cause problems" without showing an example of problems it will cause.

  4. Can anyone help explain the two loops?
    for(let i = 0; i < 3; i++) {

    setTimeout(function f() {

    console.log(i);

    },2000);

    }

    for(var i = 0; i < 3; i++) {

    setTimeout(function f() {

    console.log(i);

    },2000);

    }

  5. This it, "function scope", that's the root of the evil I feel about JS, that's why I feel so bad about it. It's also the main reason why I generally don't understand where a value comes from when I read JS : thank you very very much.
    I'm a C++/Clojure/Rust etc programmer and this is a major difference between JS and other languages in the world that distinguish JS!
    I completely understand why you prefer "block scope": you're a sane person, and I like that <3
    Thank you again for your amazing work πŸ™‚

  6. thank you dan…. you opened my eyes…. i googled about this and all it shows is both are seemigly the same …. u can use anything…

    also in p5js there the var declared in setup() cannot be reffered or used in draw()

  7. great video. I didn't even know what block scope or function scope was, just knew it was different and tricky. Now I know. thank you so much. Subscribed! looking forward to more of your videos πŸ™‚

Leave a Reply