Skip to content

Instantly share code, notes, and snippets.

@AppMkrATL
Last active March 2, 2018 21:10
Show Gist options
  • Save AppMkrATL/5bc1c559df41123218e9b5cbae26405b to your computer and use it in GitHub Desktop.
Save AppMkrATL/5bc1c559df41123218e9b5cbae26405b to your computer and use it in GitHub Desktop.
Write up your answers in a Gist. Submit a link to your Gist below to share with your mentor.
At your next session, be sure to spend some time talking about your answers.
Questions
=========
1- What is scope? Your explanation should include the idea of global vs. local scope.
2- Why are global variables avoided?
3- Explain JavaScript's strict mode
4- What are side effects, and what is a pure function?
Answers
=======
1- JavaScript has two scopes – global and local.
Any variable declared outside of a function belongs to the global scope, and is therefore accessible from anywhere in your code.
Each function has its own scope, and any variable declared within that function is only accessible from that function
and any nested functions.
Because local scope in JavaScript is created by functions, it’s also called function scope.
When we put a function inside another function, then we create nested scope.
2- The reason is that every JavaScript file included in the page runs in the same scope.
If you have global variables or functions in your code, scripts included after yours that contain the same variable and function names
will overwrite your variables/functions.
This practice will produce some undesirable results (side-effect)
3- Strict mode is a way of telling browsers (and JavaScript debuggers) to be, well, stricter about how they parse your code.
Strict mode eliminates some JavaScript silent errors by changing them to throw errors.
The 'use strict' command can be put at the top of a file to enforce strict mode for the entire file,
or at the top of a function, if you only want to use strict mode within the function body
4-
A "side effect" is any effect other than that return value
A "pure function" is a function that maps its input value(s) into an output value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment