A.2. The keywords let and const

The let or const keywords should be used as replacements for the var keyword. Let’s start by reviewing the issues with the var keyword.

A.2.1. The var keyword and hoisting

In ES5 and older versions of JavaScript, you’d use the var keyword to declare a variable, and the JavaScript engine would move the declaration to the top of the execution context (for example, a function). This is called hoisting (see more on hoisting at http://mng.bz/3x9w).

Because of hoisting, if you declared a variable inside a code block (for example, inside the curly braces in an if statement), this variable would be visible outside the block as well. Look at the following example where we declare the variable i inside a for loop but ...

Get TypeScript Quickly now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.