Block Scope

To enforce block-scoping in JavaScript, the let keyword can be used. It makes variables inaccessible from the outside of their blocks:

1
2
3
4
5
6
7
8
9
function myFunction(): void {
if (true) {
let myNumber = 1337;
}
// Will throw a `ReferenceError` because `myNumber` is not defined
return myNumber;
}

console.log(myFunction()); // Causes an uncaught `ReferenceError`