You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
```smart header="This article is for understanding old scripts"
The information in this article is useful for understanding old scripts.
That's not how we write new code.
This is not how we write new code.
```
In the very first chapter about [variables](info:variables), we mentioned three ways of variable declaration:
Expand All
@@ -20,11 +20,11 @@ var message = "Hi";
alert(message); // Hi
```
But internally `var` is a very different beast, that originates from very old times. It's generally not used in modern scripts, but still lurks in the old ones.
But internally `var` is a very different beast that originates from very old times. It's generally not used in modern scripts, but still lurks in the old ones.
If you don't plan onmeeting such scripts you may even skip this chapter or postpone it.
If you don't plan onworking with such scripts you may even skip this chapter or postpone it.
On the other hand, it's important to understand differences when migrating old scripts from `var` to `let`, to avoid odd errors.
On the other hand, it's important to understandthedifferences when migrating old scripts from `var` to `let` to avoid odd errors.
## "var" has no block scope
Expand All
@@ -42,7 +42,7 @@ alert(test); // true, the variable lives after if
*/!*
```
As `var` ignores code blocks, we've got a global variable `test`.
As `var` ignores code blocks, we have a global variable `test`.
If we used `let test` instead of `var test`, then the variable would only be visible inside `if`:
Expand DownExpand Up
@@ -85,7 +85,7 @@ sayHi();
alert(phrase); // ReferenceError: phrase is not defined
```
As we can see, `var` pierces through `if`, `for`or other code blocks. That's because a long time ago in JavaScript, blocks had no Lexical Environments, and `var` is a remnant of that.
As we can see, `var` pierces through `if`, `for`and other code blocks. That's because a long time ago in JavaScript, blocks had no Lexical Environments, and `var` is a remnant of that.
## "var" tolerates redeclarations
Expand All
@@ -109,7 +109,7 @@ alert(user); // John
## "var" variables can be declared below their use
`var` declarations are processed when the function starts (or script starts for globals).
`var` declarations are processed when the function starts (orthescript starts for globals).
In other words, `var` variables are defined from the beginning of the function, no matter where the definition is (assuming that the definition is not in the nested function).
Expand DownExpand Up
@@ -160,9 +160,9 @@ function sayHi() {
sayHi();
```
People also call such behavior "hoisting" (raising), because all `var` are "hoisted" (raised) to the top of the function.
People also call such behavior "hoisting" (raising), because all `var`declarationsare "hoisted" (raised) to the top of the function.
So in the example above, `if (false)` branch never executes, but that doesn't matter. The `var` inside it is processed in the beginning of the function, so at the moment of `(*)` the variable exists.
So in the example above,the`if (false)` branch never executes, but that doesn't matter. The `var` inside it is processed in the beginning of the function, so at the moment of `(*)` the variable exists.
**Declarations are hoisted, but assignments are not.**
Expand DownExpand Up
@@ -205,7 +205,7 @@ sayHi();
Because all `var` declarations are processed at the function start, we can reference them at any place. But variables are undefined until the assignments.
In both examples above, `alert` runs without an error, because the variable `phrase` exists. But its value is not yet assigned, so it shows `undefined`.
In both examples above, `alert` runs without an error because the variable `phrase` exists but its value is not yet assigned, so it shows `undefined`.
## IIFE
Expand All
@@ -225,9 +225,9 @@ An IIFE looks like this:
})();
```
Here, a Function Expression is created and immediately called. So the code executes right away and has its own private variables.
Here, a Function Expression is created and immediately called, so the code executes right away and has its own private variables.
The Function Expression is wrapped withparenthesis `(function {...})`, because when JavaScript engine encounters `"function"` in the main code, it understands it as the start of a Function Declaration.But aFunction Declaration must have a name, so this kind of code will give an error:
The Function Expression is wrapped withparentheses `(function {...})`, because whentheJavaScript engine encounters `"function"` in the main code, it understands it as the start of a Function Declaration.AFunction Declaration must have a name, however, so this kind of code will give an error:
```js run
// Tries to declare and immediately call a function
Expand DownExpand Up
@@ -279,9 +279,9 @@ In all the above cases we declare a Function Expression and run it immediately.
There are two main differences of `var` compared to `let/const`:
1. `var` variables have no block scope, their visibility is scoped to current function, or global, if declared outside function.
2. `var` declarations are processed at function start (script start for globals).
1. `var` variables have no block scope. Their visibility is scoped tothecurrent function, or global, if declared outside the function.
2. `var` declarations are processed atthefunction start (or at thescript start for globals).
There's one more very minor difference related to the global object, that we'll cover in the next chapter.
There's one more very minor difference related to the global object that we'll cover in the next chapter.
These differences make `var` worse than `let` most of the time. Block-level variablesis such a great thing. That's why `let` was introduced in the standard long ago, and is now a major way (along with `const`) to declare a variable.
These differences make `var` worse than `let` most of the time. Block-level variablesare such a great thing. That's why `let` was introduced in the standard long ago, and is now a major way (along with `const`) to declare a variable.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.