The let and const Keywords
Learn how to define a variable's scope and constants using the let and const keywords in TypeScript.
We'll cover the following...
We'll cover the following...
The let keyword
The fluid nature of JavaScript variables can sometimes cause errors when we inadvertently define variables with the same name but in a different scope within a code block.
Consider the following TypeScript code:
Variable index redeclared in the if-block
-
On line 2, we define a variable named
indexof typenumberusing thevarkeyword and assign it a value of0. -
We then test if this value is equal to
0on line 5, and if it is, we enter a code block. -
The first statement in this code block on line 6 defines a variable named
indexof typenumberand ...