Declaring a Variable

This lesson covers the archaic var that is supported in TypeScript and describes the cost of using this old fashion way to declare a variable. We will see how we can maximize the declaration with let and const as alternatives.

Declaring with var

Let’s get started with var. This has been the way to define variables since the inception of JavaScript. However, the release of ECMAScript2015 brought the declarations let and const, which fixed many of the drawbacks perpetuated in previous versions of ECMAScript. In many modern languages, declaring a variable is as simple as being alive in the scope where this one was defined.

For example, a variable declared in a conditional statement is only available inside the conditional statement – not before or after.

One issue with var is that the location of a variable makes it unpredictable. A variable declared using var is function-scoped when declared inside a function, but global-scoped when declared outside of a function. Also, var does not stop you from redefining the same variable, which overrides the initial declaration or initialization.

Get hands-on with 1200+ tech skills courses.