Week 3 Flashcards
what is a template literal
a new way to create a string literal that expands on the syntax of the String primitive type allowing for interpolated expressions to be inserted easily into strings.
what is the main advantage of using template literals?
the ability to interpolate variables or expressions into strings
what do we wrap the data that we want to interpolate in?
${}
what happens to the data wrapped in the dollar sign curly braces when the code is run?
the variables or expressions wrapped within the ${} will be evaluated and then replaced with the value of that variable or expression
what character do we use to create a template literal?
a backtick aka grave (`)
what can template literals evalute?
anything that can be stored in a variable including functions
what is a call stack?
a structure that JAvaScript uses in the JS runtime to keep track of the evaluation of function calls; it uses the stack data structure
what is a stack? how are we using this right now?
a general pattern of organizing a collection of items
currently the items being organized are the function calls that occur during the execution of our program
describe our current usage of a stack as a vertical pile
- pushing a new item to the stack - new items must be placed on top of the pile
- popping the top item from the stack - at any point, the only item that can be removed is the top of the pile
what does the term stack frames describe?
the items that are being pushed and popped; items placed on the call stack
describe the ways the JS leverages stack mechanics during runtime
- when a function is called, a new frame is pushed onto the stack
- when a function returns, the frame on the top of the stack is popped off the stack
when will a frame entirely leave the stack
when the function is popped due to a function return which can either be an explicit return with the return keyword or an implicit return after the last line of the function’s definition is executed
what does the function on top of the call stack represent?
the function being executed currently
when can the program on the call stack exit?
when the stack is empty
why is JS a single-threaded language?
the use of a single call stack leads to a single thread of execution; the JS runtime can only preform one command at a time and the one command currently being executed is what ever is at the top of the stack
what is the critical behavior to be aware of in the JS runtime?
an event can only be handled once the call stack is empty
what does it mean for a function to be recursive?
the function is called from within itself
what is the difference between a functtion that recurs and a function that is recursive
recur means for a function to be called more than once, while recursive means that the function is called from within itself
what are the two cases in a recurisve function and what does this term mena?
the two cases are the expected output for a particular input in a resursive function; the two cases are the base case and the recursive case
what is the base case in recursion?
when the data passed into our function is processed without any additional recursion; the base case is excuted, the function runs once and ends; the situation in which the function stops recursing
what is the base case also know as? and why
the terminating case because it results in the function stopping
what is the recursive case?
the situation where the function recurses, this represents the data state that causes the function to call itself; what causes our function to keep recursing
what is a default parameter?
I’m not sure yet
how is a default parameter declared?
it is declared in the function signature like a regular parameter, except it is given a default value using =
when can you start executing a recursive function’s stack frames
when you reach the base case
if there a limit on the number of times that a recursion is done? if yes, what is that limit
JS does have a call stack size limit which depends on how much memory is allocated to the JS program on your system
what is stack overflow
an error that is thrown from your code when you call stack reaches the call stack limit; the program is halts, the stack is wiped out entirely and you have no result
what is direct recursion?
functions directly calling themselves
what is indirect recursion
recursive loops across multiple functions
how is iterative code different from recursive code?
its less resource-intensive
requires less planning
easier to read and understnad
when should you choose recursion over iteration?
when the problem can clearly be subdivided into smaller problems by solving the smallest or simplest case and then working towards a full solution; if its a problem you can solve on paper for 1-5 things, but can’t imagine doing for 1000, use recursion
when should you use iteration over recursion
it is a problem that is no harder for 1000 things than 1-5, but will just take longer
what are the three steps that define a recursive function?
The function calls itself
The function has an end state (base case)
The function moves closer to the base case with each call (recursive step)
what is an iife?
an immediately-invoked function; it is an anonymous function that is defined and then invoked as soon as it is defined
what are the steps to writ an IIFE?
- wrap the anonympus function in the grouping operator ()
- invoke the function after the grouping operator with another set of parenthesis
how can we hang on to the result of an IIFE?
assigning the return value to a variable
do we have access to variables declared within an IIFE after the function has been called?
no
how does hoisting work, when a function is declared using function declaration syntax?
the name of the function and its contents are hoisted
explain how JS approaches hoisting when the function is declared with function declaration syntax
when JS runs this code, it takes the function declaration and the function itself in its memory to the top of the code we’re writing
can you calla afunction that is declared using function declaration syntax, before it is actually declared?
yes
how does hoisting work when a function is declared using function expression syntax and using the keywords const and let
hoisting doesn’t work here, we will get a reference error telling us that we cannot access the value of the variable before its initialization; the value of the variabel has not been assigned yet
how does hoisting work when a function is declared using function expression syntax and using the keyword var
hoisting doesn’t work here, the name of the variable is hoisted to the top of the scope, but it is assigned a value of undefined until the variable is assigned, we will get an error message because we are attempting to invoke undefined