Created
June 26, 2018 21:38
-
-
Save ticotheps/d80bf69989db4fb2614ecdb2c004a8f5 to your computer and use it in GitHub Desktop.
Revisions
-
ticotheps created this gist
Jun 26, 2018 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,65 @@ Thinkful Unit 2 - Programming in JavaScript Lesson 5 - Scope and The Problem with Globals Assignment 3 - Challenge: In Your Own Words 1) What is scope? Your explanation should include the idea of “global” vs. “local” scope. The term “scope” can be described as the set of rules that govern whether or not a variable can be referenced within different parts of your code or file. In JavaScript, the two kinds of scope that we deal with as programmers are the “global scope” and the “local/block scope.” Variables that are defined “globally” can be accessed anywhere within the code or document. On the other hand, a variable that is defined “locally” can only be accessed within the corresponding function’s block of code in which it was defined. If we were speaking in terms of U.S. government, “global scope” could be likened to “the federal level” of the U.S. government and “local/block scope” could be compared to “the state level” of the U.S. government. Just like all laws created at “the federal level” are enforced by the entire U.S. nation (including all U.S. state governments), all those variables defined at the “global scope” are accessible anywhere within the code (including inside all functions within that code). In contrast, laws that were created at “the state level” are only enforced in the U.S. states in which they were created (not in EVERY U.S. state), just as variables defined at the “local/block scope” are only accessible within the functions in which they were defined (not in EVERY function in the code). 2) Why are global variables avoided? Global variables are to be avoided as much as possible because they increase the risk of causing unintended errors with local variables that may unintentionally share the same name. For instance, a function with a variable that has been defined locally may unintentionally alter the value of a global variable that is OUTSIDE of that function, which coincidentally shares the exact same name. In turn, this alteration may also have catastrophic effects on OTHER functions in the code that may depend on the original value of that global variable to run properly. 3) Explain JavaScript’s “strict mode.” JavaScript’s “strict mode” is a feature that will alert the user with an error message upon detection of an instance where a variable is declared inside a function WITHOUT the keywords “let” or “const.” When these keywords aren’t used inside functions with variable declarations, we can have two detrimental consequences. First, if the compiler finds that the declared variable was not already globally defined, it may unintentionally create a new variable. Secondly, if the compiler did find that the declared variable was already defined globally, it may unintentionally alter the value of that global variable. In order to enable JavaScript’s “strict mode” for your entire codebase, you simply put the ‘use strict’; command at the top of all your JavaScript files. However, if you’d only like to apply “strict mode” to a specific function, you can put the ‘use strict’; command at the top of the corresponding function. 4) What are “side effects,” and what is a “pure function?” The term “side effect” refers to when a function unintentionally steps outside it’s “jurisdiction” and mistakenly alters the value of a global variable. For example, this can occur when a variable is not properly declared inside the function with the keywords “let” or “const,” causing the compiler to reach up into it’s parent scope because it cannot proper reference the variable. A “pure function” is a function that will repeatedly produce the same output values when given the same input values. This is comparable to the “reliability” measure that is commonly used in the scientific method. The measure of “reliability” in a scientific experiment is known as the ability of an experiment to repeatedly generate the exact same results when reproduced by different scientists, under the exact same conditions. Functions that are not “pure” will generate inconsistent output values despite being given the same single-set of input values. Unless we have a reason not to, we should always attempt to create pure functions.