Skip to content

Instantly share code, notes, and snippets.

@mcshakes
Last active January 26, 2018 16:50
Show Gist options
  • Save mcshakes/93cedf2b11dc3da5e3a4bc03df6fe636 to your computer and use it in GitHub Desktop.
Save mcshakes/93cedf2b11dc3da5e3a4bc03df6fe636 to your computer and use it in GitHub Desktop.
1) What is scope? Your explanation should include the idea of global vs. local scope.
I think of scope as accessibility of my variables, whether it is in a function or exists within the global sphere. Variables defined within a
a function (with var or let for example) are local variables, while variables defined outside of a function are global variables.
These variables in the global scope can be altered in any other scope. Local variables are just bound to their function (within the block) and are hidden from other
functions.
2) Why are global variables avoided?
They are accessible everywhere: from the console, from different functions. They can be overriden and the values can be fetched. This causes concern for future maintenance, as someone will have to look through entire call stack to identify which variable points to where, even using the same name and introducing duplicate bugs.
3) Explain JavaScript's strict mode
It's where JS code is executed without ignoring the wrong syntax. Personally, I've only ever seen it used as a global but could be used within a variable as well (don't see the point of that). In strict mode, you can't use an undeclared variable, as it throws an exception. I also can't declare same object properties more than once. Honestly, the only thing I've run into (besides the proper naming issue) is the issue whre "this" keyword is undefined in global functions. Strict mode seems to be just an evaluator that makes you write proper JS.
4) What are side effects, and what is a pure function?
Side effects are state changes outside the called function (not the returned value). Examples include logging to console, writing to a file, and triggering external processes. A pure function returns an output (given an input) without producing the side effects.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment