...

/

TypeScript Scope is JavaScript Scope

TypeScript Scope is JavaScript Scope

This lesson discusses the principle of shadowing, capturing, and declaring a variable in JavaScript and naturally porting to TypeScript.

We'll cover the following...

Shadowing scope

We briefly encountered the concept of scope when exploring the three declaration types discussed in the previous lessons. You saw that a variable declared using var has a broader than, let, and const. However, there are some other cases involving the scope with let and const.

The first case is shadowing. This occurs when one variable is declared twice, in an outer scope, and an inner scope. For example, if you have two loops and both of them are using the variable i, TypeScript is smart enough to understand that both declarations are for two different variables. However, it is confusing and susceptible to error, hence it is not recommended even if the code will transpile without a problem. ...