Skip to content

Commit

Permalink
Merge pull request #6 from HariAcidReign/restyled/pull-5
Browse files Browse the repository at this point in the history
Restyle Definition of Undefined is Missing
  • Loading branch information
HariAcidReign committed Oct 12, 2021
2 parents 4ba4b9a + e97d23d commit da92bcb
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions Notes/3-Hoisting.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ console.log(x);

Output:

> Namaste JavaScript
> Namaste JavaScript
> 7
> 7
```
// code example 2
getName(); // in most languages, both lines which are above their declaration will give error. Not in JS though.
console.log(x);
getName(); // in most languages, both lines which are above their declaration will give error. Not in JS though.
console.log(x);
var x = 7;
Expand All @@ -34,10 +34,11 @@ function getName(){
```

Output:
> Namaste JavaScript
Output:

> Namaste JavaScript
> undefined
> undefined
```
// code example 3
Expand All @@ -53,14 +54,18 @@ function getName(){

Output:

> Namaste JavaScript
> Namaste JavaScript
> Error: x is not defined // note that not defined here and "undefined" in sample 2 are totally different.
> Error: x is not defined // note that not defined here and "undefined" in
> sample 2 are totally different.
- Not defined: We have not initialised the value for variable anywhere in the entire code and in memory space.
- Undefined:
- Not defined: We have not initialised the value for variable anywhere in the
entire code and in memory space.
- Undefined: It is a placeholder that is assigned to a variable by the
Javascript Engine until the variable is assigned with some other value.

__Hoisting__ is a concept which enables us to extract values of variables and functions even before initialising/assigning value without getting *error*
**Hoisting** is a concept which enables us to extract values of variables and
functions even before initialising/assigning value without getting _error_

```
Expand All @@ -78,9 +83,10 @@ console.log(getName)
Output:

> f getName(){
console.log("Namaste JavaScript);
}

}

```
Expand All @@ -99,13 +105,16 @@ function getName(){
```

Output:
> Namaste JavaScript

> undefined
> Namaste JavaScript
> undefined
> f getName(){
> f getName(){
console.log("Namaste JavaScript);
}

}

```
// code example 6
Expand All @@ -116,26 +125,22 @@ var getName = function () {
console.log("Namaste JavaScript");
}
var getName = () => { // use fat arrow function
var getName = () => { // use fat arrow function
console.log("Namaste JavaScript");
}
```

Output:

> undefined //it is because they behave as variable and not function.
> undefined //it is because they behave as variable and not function.
---

__REASON OF WEIRDNESS__

* The answer lies in the Global Exection Context. In the memory phase, the variables will be initialized as *undefined* and functions will get the whole function code in their memory.

* This is the reason why we are getting these outputs.




**REASON OF WEIRDNESS**

- The answer lies in the Global Exection Context. In the memory phase, the
variables will be initialized as _undefined_ and functions will get the whole
function code in their memory.

- This is the reason why we are getting these outputs.

0 comments on commit da92bcb

Please sign in to comment.