Scope answers the question: "Where can I use this variable?"
Direct answer
Scope is where a variable is visible. JavaScript has global scope, function scope and block scope. Variables declared with let and const are block-scoped, so they only exist inside the nearest curly braces. Inner scopes can read variables from outer scopes, but not the other way around.
Examples
const globalName = "everywhere";
function demo() {
const functionName = "inside function";
if (true) {
const blockName = "inside block";
console.log(globalName, functionName, blockName);
}
console.log(blockName); // error
}
The scope chain
If JavaScript cannot find a variable in the current scope, it checks the one outside it, and so on until it reaches global.
Tips
- Keep variables as local as possible.
- Avoid global variables.
- Use
constandletinstead ofvar.
Where Teyro fits
Teyro's JavaScript for Beginners course turns this into short lessons you can play in a few minutes a day. It has 50 lessons, and you can start free.
The bottom line
Smaller scopes mean fewer surprises.



