Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save escottalexander/1ab84d178d4eb03e166657060d2b4799 to your computer and use it in GitHub Desktop.
Save escottalexander/1ab84d178d4eb03e166657060d2b4799 to your computer and use it in GitHub Desktop.
What is scope? Your explanation should include the idea of global vs. local scope.
Scope is the idea of where a variable is accessible. Global scope means that the variable resides outside of any function and as a result can be accessed by anything. Local scope would mean that the variable is accessible only inside it's original function.
Why are global variables avoided?
Global variables can cause issues because that variable can be referenced by any function in the program and may be altered accidentally, rendering it useless to its original purpose.
Explain JavaScript's strict mode
Strict mode raises a reference error when a variable is defined without let or const. It helps to keep the code collaboration friendly and bug free.
What are side effects, and what is a pure function?
Side effects are unintended issues that arise because a function is reaching outside itself and altering a different part of the program. A pure function is one that does not do this.
@Jefftopia
Copy link

Strict mode raises a reference error when a variable is defined without let or const.

Hmm, are you sure about this? Strict mode prevents accidental globals. For example,

// Suppose you're in the browser window context
n = 5; // reference error - "n" is not defined

It also prevents writing to read-only properties

Infinity = 10 // Cannot assign to read only property 'Infinity' 

But for most browsers today, let and const misuse will throw errors with or without strict mode.

A pure function is one that does not do this.

You're on the right track, but I think there's a clearer way to state this: The same input(s) always returns the same output(s).

Can you think of an example of a function in Math that's pure? How about one that isn't pure?

@escottalexander
Copy link
Author

That makes sense. I suppose most of the Math functions are pure, such as Math.round, Math.pow, and Math.sqrt. One that is not pure would be Math.random. Obviously this one is not pure because it does not give the same output when you give it the same input.

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