Created
October 8, 2019 23:32
-
-
Save oaluna/baab4dd4e72b6b7eaa7b7bb7d0ee1d9a to your computer and use it in GitHub Desktop.
Variable Scope Questions
This file contains 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 what defines a variable's access throughout certain parts of a code. If a variable's definition is within a block of instructions (like a function), then this variable is said to have block scope; its value can only be accessed within the block of instructions. If defined outside of a block of instructions, a variable is defined in the global scope, where its value can be accessed anywhere it is invoked. Whenever a definition is unavailable within the block scope, JavaScript will search up the scope chain, through parent scopes of a variable's function, for a definition. Variables declared at the global scope can be troublesome for this reason because it can lead to parent values being altered when running a code and make a function intederminate; it won't return the same value given the same input. Strict mode is a command that, when placed at the top of a JavaScript file, requires a contributor to use let or const when declaring a variable. Not doing so in Strict Mode will return an 'uncaught reference error'. Using strict mode helps write determinate functions that will return the same value given the same input. It helps avoid side effects, unintended altering of parent values in a function's scope. Functions with no side effects and that are determinate are known as pure functions, which should ideally be used unless side effects are deliberately caused. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment