JavaScript ES6 Flashcards
What is a code block? What are some examples of a code block?
A code block is a section of code that is denoted by curly braces ( { } ), also the code in parentheses of the for loop. Some examples of a code block would be functions, if else statements, for loops, while loops, etc.
What does block scope mean?
Block scope means that it is only accessible within the code block. What happens inside the code block stays inside the code block.
What is the scope of a variable declared with const or let?
Variables declared with const or let are all blocked-scope variables.
What is the difference between let and const?
The difference is that const is a read-only reference to a value and are immutable, or can’t be reassigned, while let declarations are mutable, which means you can change their values anytime.
Why is it possible to .push() a new value into a const variable that points to an Array?
This is because even though the Array is immutable, its value or its contents are not.
How should you decide on which type of declaration to use?
Each declaration should be const, unless you want them to be mutable, in which case you should use go back and change to let. Never var.
What is the syntax for writing a template literal?
Use backticks (``) instead of single or double quotes. ${…} is used for expressions (variables, functions, etc.). Only use template literal if you will use substitution.
What is “string interpolation”?
The JavaScript engine will automatically replace these variables and expressions (${…}) with their values.
What is destructuring, conceptually?
It “destructures” something by giving a name to, or assigning to a variable, each of its parts.
What is the syntax for Object destructuring?
const { prop1: variableName1, prop2: variableName2 } = object;
or if variable name will be the same as property name, then:
const { prop1, prop2 } = object
For nested object properties:
const {
name: {
firstName,
lastName
}
} = employee;
to get firstName and lastName.
What is the syntax for Array destructuring?
const [x, y, z] = array;
const [
firstName,
lastName,
[
color1,
color2,
color3
]
] = getProfile();
to get colo1, colo2, and color3.
How can you tell the difference between destructuring and creating Object/Array literals?
Creating Object/Array literals will have the variable name to the left of the assignment operator while destructuring Object/Array literals will have the variable name to the right of the assignment operator.
What is the syntax for defining an arrow function?
Instead of
function (param) {
return value;
}
param => value;
When an arrow function’s body is left without curly braces, what changes in its functionality?
It returns the function body. It also won’t be able to have an object literal since both block and object literal use curly brackets, so JavaScript can’t distinguish between a block and an object. Instead, you must wrap the object literal in parentheses.
How is the value of this determined within an arrow function?
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?
.then(onFulfillment)
How do you handle the rejection of a Promise?
.then(…, onRejection)
or
.catch(onRejection)
What does Array.filter do?
The filter() method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.
What should the callback function return?
A boolean, true or false.
What is Array.filter useful for?
It is useful for creating a new array that contains elements that you care about or pass a test case.
What does Array.map do?
The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.
What should the callback function return?
An array element that has been mutated with the provided function.
What is Array.map useful for?
It is useful for creating a new array that has had something done to each element from the original array. Change and array into react components.