From the course: Learn JavaScript: Write Modern Code with JavaScript ESNext

Unlock this course with a free trial

Join today to access over 25,000 courses taught by industry experts.

Learn the difference between block-scoped and function-scoped variables

Learn the difference between block-scoped and function-scoped variables - JavaScript Tutorial

From the course: Learn JavaScript: Write Modern Code with JavaScript ESNext

Learn the difference between block-scoped and function-scoped variables

- Earlier in the course when we were first looking at how to define variables in JavaScript, we saw that there were two keywords for defining variables, and that was var and let. At the time I said that we should almost always err on the side of using let instead of var. And that variables defined with var were what's called function-scoped while variables defined with let were block-scoped. And I think that now is a good time to elaborate a little bit on what exactly block and function scoping means. So for this example, instead of using the Chrome console, I'm going to use a website called repl.it, which provides a nice way to play around with running JavaScript in a more file-oriented way. You can do this as well by going to repl.it. And up in the top-right corner here, you're going to click new repl and search for JavaScript and hit Create. And that'll take you here. So with function-scoped and block-scoped variables, the difference here is actually a fairly simple one. Variables…

Contents