Loading topic
total, user, or render, JavaScript has to work out which variable or function that name refers to. Scope is the set of rules that answers this question. A variable's scope is the region of code where you can refer to it by name.2 min readvar, let, and const, write functions, and run code in a browser console or the Node.js REPL. The previous chapter covered lexical scope in general. Here we look at the outermost scope, which every other scope sits inside.3 min readvar, let, and const. It also assumes you know that lexical scope means visibility is decided by where code is written, and that the global scope is the outermost level.2 min readvar, let, and const, write if statements and for loops, and already know from the previous chapter that var is scoped to the enclosing function. Here you will see how let and const add a smaller unit of scope: the block.2 min readvar, let, and const, and can write nested functions. It also builds on the earlier chapters about global, function, and block scope. The question here is how JavaScript finds a variable when code uses its2 min readvar, let, and const, and that you know how the scope chain searches outward from the current scope for a name. Shadowing is what happens when that search finds a match earlier than you expected.2 min readvar, let, and const, write and call functions, and run code in a browser console or Node.js. It also builds on the earlier chapters about function scope, block scope, and the scope chain.3 min readvar, understand function scope, and have read the previous chapter on the creation and execution phases of a scope. Here you will apply that two-phase model to var and see the bugs it causes.2 min readvar is hoisted during the creation phase of a scope, and that you can write and call functions. You will see why some functions can be called before the line that defines them while others fail, and what kind of error you get when it goes wrong.2 min readvar hoisting works and that each scope has a creation phase followed by an execution phase. You should also know that let and const are block-scoped.2 min readlet and const. You should also be able to run code in Node.js or a browser console.2 min readvar is function-scoped and let is block-scoped. Here we combine those ideas to explain one of the most common bugs in JavaScript.2 min readvar behaves at the top level of a script. It covers one quirk of name resolution: what happens when you assign to a name that was never declared.2 min read<script type="module">.3 min read