diff --git a/Notes/3-Hoisting.md b/Notes/3-Hoisting.md index c42b0b9..5529086 100644 --- a/Notes/3-Hoisting.md +++ b/Notes/3-Hoisting.md @@ -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; @@ -34,10 +34,11 @@ function getName(){ ``` - Output: -> Namaste JavaScript +Output: + +> Namaste JavaScript -> undefined +> undefined ``` // code example 3 @@ -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_ ``` @@ -78,9 +83,10 @@ console.log(getName) Output: > f getName(){ + console.log("Namaste JavaScript); - } +} ``` @@ -99,13 +105,16 @@ function getName(){ ``` Output: -> Namaste JavaScript -> undefined +> Namaste JavaScript + +> undefined + +> f getName(){ -> f getName(){ console.log("Namaste JavaScript); - } + +} ``` // code example 6 @@ -116,7 +125,7 @@ var getName = function () { console.log("Namaste JavaScript"); } -var getName = () => { // use fat arrow function +var getName = () => { // use fat arrow function console.log("Namaste JavaScript"); } @@ -124,18 +133,14 @@ var getName = () => { // use fat arrow function 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.