Skip to content

Instantly share code, notes, and snippets.

@camckin10
Last active December 5, 2016 15:49
Show Gist options
  • Select an option

  • Save camckin10/835b287e903ba4fca5a653665eb6f1b4 to your computer and use it in GitHub Desktop.

Select an option

Save camckin10/835b287e903ba4fca5a653665eb6f1b4 to your computer and use it in GitHub Desktop.
Challenge: In Your Own Words
What is scope? Your explanation should include the idea of global vs. local scope.
With variables, there are two types: global and local. A global scope is a variable that is declared in the main body source of your code.
For example, imagine you have an JS file that has a variable declared *before* there is a function. Your global scope
would look like this: var schoolName="Island"; A local scope is a variable that has been declared and is *only* accessible through a set of
instructions, such a function. For example, if you were to have a JS file, and in this JS file there is a function called "mySchool."
Then your local scope in your JS file would look like this: function mySchool()
var schoolNames="Island";
Why are global variables avoided?
Global variables are avoided because they can make your code extremely messy. Global variable or scope that is declared can be available
everywhere, outside of a function. That being said, if you were to use a global variable, and then a local variable with the same name,
the global variable can cause the computer to get confused. As well as causing the computer to get confused, a global scope can cause
side effects to a function which would alter the value of the parent scope.
Explain JavaScript's strict mode
JS strict mode is a mode that is activated in JS that puts restrictions on your JS code. How strict mode changes JS is that it eleminates
errors, fixes mistakes that make the JS engine hard to run (i.e.global scope), and stops from syntax from being defined again. There are
two possible ways to invoke strict mode in your JS file.
One, you could invoke strict mode to an entire script.
For example, "Use strict";
var b = "Life is roses.";
Two, you could invoke strict mode by a function to function basis.
For this second option, you would still add the string, "use strict", but you would put that string in your function instead of *outside* the function.
For example, function life(){
"use strict";
function nested(){
return , "life is nice,";}
}
Overall, there are programs now that will automatically put your code into strict mode.
What are side effects, and what is a pure function?
A side effect in JS is any other effect than that return value. The most concern about creating a side effect is that the code will live
beyond its execution period of the function. Some side effects make functions harder to reuse.
A pure function is function that maps the input value into an output value. Pure functions produce NO side effects, and they do not rely
on an external state, such a side effect. Pure functions are the simplest way of creating reusable code blocks. Pure functions are also
independent, and serve to do one thing, and to do it well.
Explain variable hoisting in JavaScript.
Hoisting is a very rare thing that will appear in your JS code at times. Essentially, hoisting is what you would call variables in your JS
code *before* you declare them. JS interpreter, console reads the code first, and then executes on the second time. That being said,
hoisting can move the declarations to the top of the code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment