Skip to content

Instantly share code, notes, and snippets.

@ElenaG518
Created July 11, 2018 00:30
Show Gist options
  • Save ElenaG518/920d8e6cf4e1a09e1b7175ad4dfc589d to your computer and use it in GitHub Desktop.
Save ElenaG518/920d8e6cf4e1a09e1b7175ad4dfc589d to your computer and use it in GitHub Desktop.
Variable Scope
What is scope? Your explanation should include the idea of global vs. local scope.
Scope means where the variable lives and how it can be accessed. the value of global scoped variables var and let can be accessed and modified by any
function that refers to it. When you declare a variable in the global scope, it is available everywhere in your code.
Any variable that is declared outside of a function in JavaScript has global scope. When you declare a variable within a function,
that variable only lives within the function, meaning it's scope is local. This means that when you define a variable in a function that
has the same name as a variable in the global scope, the local variable will take precedence over the global one.
Why are global variables avoided?
Global variables should be avoided because they could inadvertently be reassigned a value by a function that refers to it,
even functions in a different file in which the global variable is being defined. This could create problems in the expected
result of various functions that use that variable.
Explain JavaScript's strict mode
it is declared by the line 'use strict' and it can be put at the top of the JS file, or within each function. It's causes
an error if the file has any variables that have not been explicitly created with the command var, let, or const.
What are side effects, and what is a pure function?
side effects can occur when a global variable is inadvertently reassigned a value by a function that uses the same name as a
global variable for a local variable, without using the keywords let or const. This can lead to indeterminate functions that
has some values sometimes and another at other times.
A function is pure when it is both determinate, meaning it gives the same result every single time it is provided the same input,
and has not side effects. Strive for pure functions!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment