Skip to content

Instantly share code, notes, and snippets.

@lauras12
Last active May 14, 2020 17:50
Show Gist options
  • Save lauras12/374eac0ba763c8ac6642beb09e72287e to your computer and use it in GitHub Desktop.
Save lauras12/374eac0ba763c8ac6642beb09e72287e to your computer and use it in GitHub Desktop.
Answers to questions about variable scope
What is scope? Your explanation should include the idea of global vs. block scope.
Scope means the variable of access, how the variables you declare can or cannot be accessed throughout your code. When a piece of scope is running, you are able to ask, ‘what variables do I have access to?’ JavaScript also follows the scope chain to determine the value of the variable. Scope chain can be described as the process of chain of references to look for variables in the program. When the interpreter encounters a variable name, it looks through the different levels of scope to find the value, starting with the local and working outwards toward the global scope. If a variable is not defined in any scope, JavaScript will throw a name error.
Even though you want to avoid using them as much as possible, global scope refers to the variable(s) declared outside of the function. When you declare a global variable, you are able to access it everywhere in your code. You can even access global variables across different files. The only exception case where you can use globals is when you are using a JavaScript library.
Block scope means that when you declare a variable in a function’s block of instructions, it is only accessible within the function’s block of instructions. When the function is done executing, the variable is no longer accessible.
Why are global variables avoided?
Global variables are to be avoided because globals can make unintended side effects in a program more likely. When side effects are unintended and you use global variables, the code becomes indeterminate which can in result lead to confusing and hard-to-track-down bugs in the code and also using global variables makes it very hard to collaborate with others on a project. This use can also mean that a global variable could be changed unintentionally by someone else's code. All functions should be kept determinate (no matter what happens outside the function, if given the same inputs, it will always return the same value) when possible.
Explain JavaScript's strict mode
JavaScipt’s strict mode is a feature that allows you to place your code in “strict” operating context. It eliminates silent errors by changing them to throw errors. The use of strict mode does not allow the programmer to declare a variable with using ‘let’ or ‘const’ keywords and a new variable is always being created within a function instead of potentially as a global variable. You can enable strict mode by including the line `'use strict';` at the top of your file (or function, but best practice says top of file is preferred).
What are side effects, and what is a pure function?
Side effects are when a function reaches outside its local scope up into apparent scope and alters a value that lives there. It makes troubleshooting code very difficult. A pure function is one that is both free of side effects and is determinate (given the same input, they always produce the same output).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment