
上QQ阅读APP看书,第一时间看更新
Variable scope (var, let, and const)
When we declare a variable in TypeScript, we can use the var, let, or const keywords:
var myNumber: number = 1; let isValid: boolean = true; const apiKey: string = "0E5CE8BD-6341-4CC2-904D-C4A94ACD276E";
Variables declared with var are scoped to the nearest function block (or global, if outside a function block).
Variables declared with let are scoped to the nearest enclosing block (or global, if outside any block), which can be smaller than a function block.
The const keyword creates a constant that can be global or local to the block in which it is declared. This means that constants are block-scoped.
You will learn more about scopes in Chapter 6, Understanding the Runtime.