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.
What is currying in regards to how we write our code?
Currying is a paradigm of functional programming in which functions are created in such a way that they accept one argument and have no side-effects.
How do we curry a function that would accept multiple arguments?
To curry a function, we nest multiple functions, each that take a single argument, and that in turn pass that argument onto further nested functions.
Why do we curry functions in Javascript?
Currying allows us to create code in which it is difficult to pass less than the required arguments into a function, making the code easier to debug.
What does curring rely on to make variables from nested functions available to each other?
Currying relies on the concept of lexical scope in order for variables to be available to one another.
Where are let and const variables hosited to in JavaScript?
let and const are hoisted to the top of their parental scope in Javascript.
Why is Set (as in new Set) useful in Javascript?
new Set will create an array of unique values from another array.
How do we destructure an object into variables
we use a variable declaration (let or const) with curly braces holding the variable names that correspond to the object properties, and set that whole equal to the object name.
const {name, year, actor} = favoriteFilm
What are the five parts of the Event Loop?
- Memory Heap
- Call Stack
- Event Queue
- Event Loop
- Node / Web API’s
What is the Heap used for in regards to JavaScript?
The Heap is a block of memory in which variables and objects are stored in an unordered manner.