<commit_before># Variable Hoisting

> A large number of JavaScript interview questions, if not most of them, can be answered with an understanding of scoping, how `this` works, and hoisting.

```js
var value = 2;

work();

function work () {
    console.log(typeof value);
    console.log(value);
    var value = 3;
}
```

You might be expecting the method to print 'number' first, and 2 afterwards, or maybe 3? Try running it! Why does it print `'undefined'` and then `undefined`? Well, hello hoisting! It'll be easier for you to picture it if I re-arrange the code to how it ends up after hoisting takes place. Let's have a look.

```js
var value;

function work () {
    var value;
    console.log(typeof value);
    console.log(value);
    value = 3;
}

value = 2;

work();
```

So, that's why: the `value` declaration at the end of the `work` function actually got hoisted to the top of the scope, and also the reason why `work` didn't meet us with a `TypeError` exception warning us about `undefined` not being a function. Keep in mind that if we used the variable form of declaring the `work` function, we would in fact have gotten that error, because although `var work` would've been hoisted, the assignment wouldn't have been, effectively becoming the following:

```js
var value;
var work;

value = 2;

work();

work = function () {
    var value;
    console.log(typeof value);
    console.log(value);
    value = 3;
};
```

The above wouldn't work as expected, because work isn't defined by the time we want to invoke it. At this point you should feel pretty comfortable with scopes and the `this` keyword. It's time to talk about closures, and modular patterns in JavaScript.

JavaScript variables fill a particular scope in the order described below.

- Scope context variables: `this`, and `arguments`
- Named function parameters: `function (these, variable, names)`
- Function expressions: `function something () {}`
- Local scope variables: `var foo`<commit_msg>wording<commit_after># Variable Hoisting

> A large number of JavaScript interview questions, if not most of them, can be answered with an understanding of scoping, how `this` works, and hoisting.

```js
var value = 2;

work();

function work () {
    console.log(typeof value);
    console.log(value);
    var value = 3;
}
```

You might be expecting the method to print 'number' first, and 2 afterwards, or maybe 3? Try running it! Why does it print `'undefined'` and then `undefined`? Well, hello hoisting! It'll be easier for you to picture it if I re-arrange the code to how it ends up after hoisting takes place. Let's have a look.

```js
var value;

function work () {
    var value;
    console.log(typeof value);
    console.log(value);
    value = 3;
}

value = 2;

work();
```

So, that's why: the `value` declaration at the end of the `work` function actually got hoisted to the top of the scope, and also the reason why `work` didn't meet us with a `TypeError` exception warning us about `undefined` not being a function. Keep in mind that if we used the variable form of declaring the `work` function, we would in fact have gotten that error, because although `var work` would've been hoisted, the assignment wouldn't have been, effectively becoming the following:

```js
var value;
var work;

value = 2;

work();

work = function () {
    var value;
    console.log(typeof value);
    console.log(value);
    value = 3;
};
```

The above wouldn't work as expected, because work isn't defined by the time we want to invoke it. At this point you should feel pretty comfortable with scopes and the `this` keyword. It's time to talk about closures, and modular patterns in JavaScript.

JavaScript variables fill the scope in the order described below.

- Scope context variables: `this`, and `arguments`
- Named function parameters: `function (these, variable, names)`
- Function expressions: `function something () {}`
- Local scope variables: `var foo`
{'fraction_non_alphanumeric': 0.09074778200253486, 'fraction_numerical': 0.004055766793409379, 'mean_word_length': 4.033163265306122, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5493836', 'n_tokens_mistral': 1127, 'n_tokens_neox': 1066, 'n_words': 572}