Scope
let and const are block scoped: they exist only inside the curly braces where they are declared. A variable declared outside any function is global and can be read everywhere, but too many globals make code hard to manage.
A function can read variables from the outside, but code outside cannot read variables declared inside the function.
Example
let outside = "I am global";
function demo() {
let inside = "I am local";
console.log(outside);
console.log(inside);
}
demo();
if (true) {
let blockOnly = 1;
}
console.log(typeof blockOnly);Exercise
Which keyword gives a block scoped variable that can change?
count = 0;Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.