Last active
March 4, 2019 17:11
-
-
Save krcrawford/507f4010bfe1009e4149599b0552a409 to your computer and use it in GitHub Desktop.
Javascript Homework #2
This file contains 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
/** | |
* | |
* Hoisting... | |
* | |
* In es5 and es6 variables are hoisted. This means that the interpreter | |
* parses files or code for variable declarations and moves them to the | |
* top of the top of their respective scopes, or "hoists" them. | |
* Variables are declared automatically, but may or may not be initialized | |
* based on how the user wites their code. Initially, any variable that is not | |
* assigned is given the default value of `undefined`, i.e typeof === 'undefined'. | |
* This applies to global scope and function scope. | |
* When using javascript's strict mode, referred to vars that are unassigned | |
* return a Reference Error, meaning they are not simply initialized as undefined. | |
* `let` when initialized, but unassigned, will produce `undefined` instead of | |
* a Reference Error. | |
* `const`, like `let`, will also produce a Reference Error. The common thread | |
* here is that in es6, when accessing uninitialized variables, regardless of | |
* operating mode, the interpreter will return a Reference Error. | |
* The difference with `const` is that the interpreter will always return a | |
* Reference Error unless the `const` is both initialized AND assigned. | |
* | |
* var: | |
* | |
* This use of `var` is the popular es5 syntax used for variable | |
* initialization. The drawback to this approach, is that there is no scope | |
* confinement in conditional statements. Take the following example: | |
* | |
* var foo = '123'; | |
* | |
* function func1() { | |
* var bar = true; | |
* if (bar) { | |
* var baz = '123'; | |
* } | |
* | |
* console.log(baz); | |
* } | |
* | |
* func1(); | |
* | |
* Here, if the `if (bar) {...` statement evaluates to true, then `baz` is | |
* initialized and assigned. Moving further down in the code, we can still | |
* access that variable. Note the `console.log(baz)`. The major drawback is that | |
* this variable can live beyond its immediate block scope. While global scope | |
* is generally not a good practice, in some cases we would not want even a | |
* function-level scope as well. In short, var is somewhat uncontrollable in the | |
* sense of scope. | |
* | |
* `let` and `const` | |
* `let` is newly introduced in es6, and sets the precendent, along with | |
* `const`, also newly introduced for es6, for true block-level variable scoping. | |
* The difference between `let` and `const` is that `let` is reassignable, | |
* whereas `const` is not. Both of these cannot be accessed in scopes outside of | |
* their current respctive blocks. | |
* | |
* let: | |
* | |
* function func2() { | |
* let foo = '123'; | |
* | |
* if (foo === '123') { | |
* let bar = '456'; | |
* foo += bar; | |
* } | |
* | |
* console.log(foo); <--- "123456" | |
* console.log(bar); <--- not defined, as it is only available to its local | |
* block | |
* } | |
* | |
* func2(); | |
* | |
* | |
* const: | |
* | |
* function func3() { | |
* const foo = '123'; | |
* foo += '456'; <--- while it stays in the current block, it still cannot be | |
* reassigned | |
* } | |
* | |
* func3(); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment