Intermediate JavaScript Flashcards

1
Q

What do we mean when we say a function has side effects?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Do “pure functions” have side effects?

A

No, “pure functions” do not have side effects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are some examples of side effects in JavaScript functions?

A
  1. Mutation of a local static variable
  2. Mutation of a non-local variable
  3. Mutation of a mutable reference argument
  4. Production or Mutation of input/output streams
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why are “pure functions” considered good?

A

They tend to be more reusable and more clear in their effect.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why might side effects be considered “tricky”

A

Side effects make the function harder to reason about (less clear) and less easy to reuse.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why is the consideration of side effects important when using JavaScript functions

A

Side effects should be considered because some functions are useful because of their side effect, while others may cause unintended actions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Name a JavaScript function primarily used for its side effects

A

The forEach array method is primarily used for its side effects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is referential transparency?

A

It means that you can replace a function call with the resulting value and not change the meaning of the program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is function composition?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What system or construct does Javascript use to handle asynchronous function calls?

A

It uses the event loop to handle asynchronous function calls.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How is the “call stack” used in relation to running Javascript code?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the “call stack” in relation to JavaScript code?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How many arguments does an event listener take?

A

An event listener only takes one argument - the event object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What happens to an event object in an event listener?

A

The event object is passed to the listener and the return value is ignored.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

If you change a variable value within an event listener function, is that change available outside of the function?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is currying in regards to how we write our code?

A

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.

17
Q

How do we curry a function that would accept multiple arguments?

A

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.

18
Q

Why do we curry functions in Javascript?

A

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.

19
Q

What does curring rely on to make variables from nested functions available to each other?

A

Currying relies on the concept of lexical scope in order for variables to be available to one another.

20
Q

Where are let and const variables hosited to in JavaScript?

A

let and const are hoisted to the top of their parental scope in Javascript.

21
Q

Why is Set (as in new Set) useful in Javascript?

A

new Set will create an array of unique values from another array.

22
Q

How do we destructure an object into variables

A

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

23
Q

What are the five parts of the Event Loop?

A
  1. Memory Heap
  2. Call Stack
  3. Event Queue
  4. Event Loop
  5. Node / Web API’s
24
Q

What is the Heap used for in regards to JavaScript?

A

The Heap is a block of memory in which variables and objects are stored in an unordered manner.

25
Q

What is the Call Stack used for in JavaScript?

A

The Call Stack tracks what function is currently being run.

26
Q

What happens when we invoke a function in regards to the Call Stack?

A

When a function is envoked, a frame is added to the Call Stack. The frame connects that function’s arguments with local variables from the Heap. Once the function runs, and is removed from the Call Stack, the frame is cleared.

27
Q

What is the Event Queue in JavaScript?

A

The Event Queue holds a list of messages corresponding to functions that are waiting to be processed.

No code is executed in the Event Queue.

28
Q

What is the Event Loop in regards to JavaScript?

A

Messages waiting in the Event Queue are added to the Stack via the Event Loop.

29
Q

What is the Memory Life Cycle in regards to JavaScript?

A

The Memory LIfe Cycle is the processing, allocation, using, and releasing of memory in regards to program execution in JavaScript.

30
Q

What is the primary issue in regards to memory in JavaScript?

A

Memory leaks, or when things are retained in memory longer than needed.

31
Q

What are the two data structures used for memory in JavaScript?

A

The Heap

The Stack

32
Q

What type of memory objects is the Stack used for in JavaScript?

A

The Stack is used for static storage, where the size of an data structure is known when the code is compiled.

Primitive values, references to non-primitive values, and function call frames are stored in the stack

33
Q

What types of memory objects are stored in the Heap?

A

The Heap provides dynamic memory storage for data structures that don’t have a fixed size, like objects and functions.

While the object itself is stored in the Heap, the reference to the object is stored in the Stack.

34
Q

What is a memory leak in JavaScript?

A

A memory leak is when memory, that is not longer needed by the program, persists.

35
Q

What are common causes of memory leaks in Javascript code?

A
  1. Messy Closures
  2. Dangling Timers and Event listeners
  3. Circular References
  4. Declaring Variables on the global object
    5.
36
Q

How does JavaScript use the Stack to manage memory?

A

The stack is used for static stoage, where the size of an object is know when the code is compiled.

37
Q

How does JavaScript use the Heap for memory management?

A

The heap provides dynamic memory allocation at runtime. Data types that don’t have a fixed size, like objects and functions, are stored in the heap.

38
Q

How does the reference-counting algorithm help with memory management in JavaScript?

A

If a reference count for an object drops to zero, there are no more references to that object in the program and it can be discarded.

39
Q

How does Mark and Sweep help with memory management in JavaScript?

A

From the root the algorithm sweeps across the code to find anything reachable by traversing the code from all variables. Anything that is not reachable is discarded