Skip to content

Instantly share code, notes, and snippets.

@sanjeev-io
Created December 28, 2018 15:06
Show Gist options
  • Save sanjeev-io/d2a4e569a92dd1aa932c870181328bd8 to your computer and use it in GitHub Desktop.
Save sanjeev-io/d2a4e569a92dd1aa932c870181328bd8 to your computer and use it in GitHub Desktop.

1. What is scope? Your explanation should include the idea of global vs. block scope. A scope is locale accessible to a variable based on where its declared. There can only be two scopes, global or local. A local scope is the one where variable is declared within a function, whereas a global scope if for varaibles declared anywhere else outside of a function. Global scopes can be mutated from anywhere in the code file or files, whereas the local scoped variables can only be changed within the function and then it will disappear after the function is invoked, without making any changes to the outside world. Its always recommended to create local scoped variables for reliable code, unless in exceptional edge cases where global scoped variable is a must.

Why are global variables avoided? It allows for mutation of variables in unknown places, making the code prone to unintended results, and hence making entire code blocks diifficult to debug.

Explain JavaScript's strict mode It has a main benefit of disallowing creation of global variables when a variable is created without let / const keyword.

What are side effects, and what is a pure function? A side effect is when unintended alteration happens to a variable value because its in global scope. A pure function is when the function always returns exact same result based on same conditions, regardless of where / how the function is accessed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment