Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

corrected translation to english errors in 'var' lesson #456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tgentry300 wants to merge 1 commit into javascript-tutorial:master from tgentry300:master
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions 1-js/06-advanced-functions/04-var/article.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ In the very first chapter about [variables](info:variables), we mentioned three

But `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 meeting such scripts you may even skip this chapter or postpone it, but then there's a chance that it bites you later.
If you don't plan on reading or editing such scripts you may even skip this chapter, but there is a chance that you will see `var` in old scripts that you are reading and/or editing.
Copy link
Member

@iliakan iliakan Aug 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double wording: reading/editing.


From the first sight, `var` behaves similar to `let`. That is, declares a variable:

Expand Down Expand Up @@ -74,9 +74,9 @@ sayHi();
alert(phrase); // Error: 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 reminiscence of that.
As we can see, `var` pierces through `if`, `for` or other code blocks. That's because in previous versions of JavaScript, blocks had no Lexical Environments. And `var` is a reminiscence of that.

## "var" are processed at the function start
## "var" variables are processed at the start of the function

`var` declarations are processed when the function starts (or script starts for globals).

Expand Down Expand Up @@ -126,7 +126,7 @@ function 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` variables are "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.

Expand Down Expand Up @@ -169,16 +169,16 @@ function sayHi() {
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.
Because all `var` declarations are processed at the function start, we can reference them at any place. But variables are undefined until they are explicitly defined.
Copy link
Member

@iliakan iliakan Jul 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undefined until they are explicitly assigned (=).

davegregg reacted with thumbs up emoji

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`.

## Summary

There are two main differences of `var`:

1. Variables have no block scope, they are visible minimum at the function level.
2. Variable declarations are processed at function start.
1. `var` Variables have no block scope, they are not block-local and can be accessed from any point in the script.
Copy link
Member

@iliakan iliakan Jul 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong, they are function-local, so can't be "accessed from any point in the script" =)

Copy link
Member

@iliakan iliakan Jul 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if rephrasing is good here...

Copy link
Contributor

@davegregg davegregg Jul 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some rephrasing is good here. In this case, I had intended to submit another PR to add precision.

Copy link
Contributor

@davegregg davegregg Jul 5, 2018
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps: Variables declared with `var` have function-level scope, instead of block-level scope.

Copy link
Member

@iliakan iliakan Jul 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

2. `var` Variable declarations are processed at function start.

There's one more minor difference related to the global object, we'll cover that in the next chapter.

Expand Down

AltStyle によって変換されたページ (->オリジナル) /