Intermediate JavaScript Flashcards
What do we mean when we say a function has side effects?
We mean that, aside from the return value of a function, a function causes the writing of a state that extends beyond the execution period of the function or an IO event.
Do “pure functions” have side effects?
No, “pure functions” do not have side effects.
What are some examples of side effects in JavaScript functions?
- Mutation of a local static variable
- Mutation of a non-local variable
- Mutation of a mutable reference argument
- Production or Mutation of input/output streams
Why are “pure functions” considered good?
They tend to be more reusable and more clear in their effect.
Why might side effects be considered “tricky”
Side effects make the function harder to reason about (less clear) and less easy to reuse.
Why is the consideration of side effects important when using JavaScript functions
Side effects should be considered because some functions are useful because of their side effect, while others may cause unintended actions.
Name a JavaScript function primarily used for its side effects
The forEach array method is primarily used for its side effects
What is referential transparency?
It means that you can replace a function call with the resulting value and not change the meaning of the program
What is function composition?
Function composition is the idea of combing functions to produce a new function or deeper computation. Think of the way React is compositional in nature; using components.
What system or construct does Javascript use to handle asynchronous function calls?
It uses the event loop to handle asynchronous function calls.
How is the “call stack” used in relation to running Javascript code?
As javascript runs and comes across a function, that function is added to the call stack. Javascript executes the function, adding any contained function calls further up the stack (stacking them up).
When javascript is finished with the function execution it removes the function from the call stack and continues with the rest of the code base.
What is the “call stack” in relation to JavaScript code?
The call stack is how the Javascript interpreter keeps track of its location within a script that calls multiple functions, what function is currently being run, and which functions are called from within that function
How many arguments does an event listener take?
An event listener only takes one argument - the event object
What happens to an event object in an event listener?
The event object is passed to the listener and the return value is ignored.
If you change a variable value within an event listener function, is that change available outside of the function?
No. This change is not available outside of the event listener function because by the time the event listener would execute, the scope in which it was defined would have already finished executing.