Created
March 5, 2019 18:41
-
-
Save jamster10/02011cf2d20204ef490fe2ba03a9bf7f to your computer and use it in GitHub Desktop.
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 characters
Scope is a way of understanding the locality of variables in a Javascript environment. A function is an enclosed program, and thus has it's own private (to the outside environment) scope called it's local scope. A variable declared within a function exists only within that function. This has the benefit of limiting any side-effects to the outside environment as the function only acts upon variables within itself. Of course, functions within said function (nested functions) do have access to the variables "above" them. Global variables on the other hand are variables that exist outside the body of any function in particular. It's a variable that all functions will see as "above" themselves, and thus have access to it. The problem with this, is that during execution, if many functions have access to a variable, it becomes difficult to track where errors might arise in a variable's value being changed, and that change may be unwanted. As this global variable exists outside of these functions, mutating it is called a side effect, and is usually frowned upon for reasons mentioned above. While there are scenarios where this is the expected and wanted outcome, such as modifying a database, it is usually best practice to avoid having side-effects. A function that causes no (unwanted) side effects, and is determinate (that is, for any given input there is an expected output as opposed in indeterminate), is said to be a pure function. | |
Javascript has a mode called strict mode, which forces the compiling engine to require strict javascript syntax. This method enforces variable types such as let and const, prevents variable name hoisting, as well as preventing global variable functionality. 'use strict' which can be enabled in any js document by declaring 'use strict' at the top, is seen as best practice. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment