var does not have block scope and, in the past, that led developers to use a JavaScript design pattern known as Immediately Invoked Function Expression (IIFE) or the Self-Executing Anonymous Function. This pattern was also used to hide variables and functions from outside visibility.

Immediately Invoked Function Expression (IIFE)

In this pattern, lines of code are wrapped inside an anonymous function that is immediately executed.

Why IIFE

For example, in the following code, the variable sqrt, defined within the block, is hoistedHoisting to the top of the file and becomes unintentionally available outside of the intended scope.

Get hands-on with 1400+ tech skills courses.