Senior JS Flashcards
What is a code block? What are some examples of a code block?
In JavaScript, blocks are denoted by curly braces {} , for example the if else, for, do while, while, try catch and so on:
What does block scope mean?
variable definition is only valid within the block of code that it was declared in
What is the scope of a variable declared with const or let?
block scoped
What is the difference between let and const?
Let can be reassigned and const can’t
Why is it possible to .push() a new value into a const variable that points to an Array?
It is adding to the array instead of reassigning the variable
How should you decide on which type of declaration to use?
If the variable does not need to to reassigned, use const
What is the syntax for writing a template literal?
Put it inside backticks, ${} for expressions and variables
What is “string interpolation”?
String formatting: the ability to substitute part of the string for the values of variables or expressions
What is destructuring, conceptually?
Taking out part of an object and assigning it to new variables
What is the syntax for Object destructuring?
const { title, author, libraryID } = book1
What is the syntax for Array destructuring?
const [book3, book4, book5] = getBooks()
How can you tell the difference between destructuring and creating Object/Array literals?
the side of the assignment operator that the object/array is on
What is the syntax for defining an arrow function?
(parameters) => code block
() not needed with one parameter
() are needed with no parameter
{} needed for more than one line of code
When an arrow function’s body is left without curly braces, what changes in its functionality?
You don’t get a return statement
How is the value of this determined within an arrow function?
This is defined at definition time for arrow functions.
For other functions, this is determined at call time
What are the three states a Promise can be in?
pending: initial state, neither fulfilled nor rejected.
fulfilled: meaning that the operation was completed successfully.
rejected: meaning that the operation failed.
How do you handle the fulfillment of a Promise?
attach a then method handler with callback function with success message
How do you handle the rejection of a Promise?
attach a catch method handler with callback function with error message
What is Array.prototype.filter useful for?
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
What is Array.prototype.map useful for?
The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.
What is Array.prototype.reduce useful for?
To execute a reducer function (that you provide) on each element of the array, resulting in single output value.
What is “syntactic sugar”?
making code easier to read
What is the typeof an ES6 class?
function
Describe ES6 class syntax.
class Stopwatch { constructor(elapsedSeconds) { this.elapsedSeconds = elapsedSeconds; }
tick() {
this.elapsedSeconds++;
}
getTime() { const secondsPerHour = 3600; const secondsPerMinute = 60; let seconds = this.elapsedSeconds; let hours = Math.floor(seconds / secondsPerHour); seconds -= (secondsPerHour * hours); let minutes = Math.floor(seconds / secondsPerMinute); seconds -= (secondsPerMinute * minutes); hours = hours.toString().padStart(2, '0'); minutes = minutes.toString().padStart(2, '0'); seconds = seconds.toString().padStart(2, '0'); return `${hours}:${minutes}:${seconds}`; }
reset() {
this.elapsedSeconds = 0;
}
}
What is “refactoring”?
making code more efficient without changing its behavior
What must the return value of myFunction be if the following expression is possible?
myFunction()();
It returns the function
What does this code do? const wrap = value => () => value;
assigns the return of the value function being called twice to the const wrap variable
Not sure, look at video
What allows JavaScript functions to “remember” values from their surroundings?
Closures