ES6 Flashcards
What is a code block? What are some examples of a code block?
A declaration that is used to group zero or more statements, in curly braces
An example: function, for loops, conditionals, etc.
What does block scope mean?
If a variable is declared inside a block scope, it is locally scoped to that block
What is the scope of a variable with const or let?
Block-scoped.
What is the difference between let and const?
Let variables are mutable, while const are not. Cannot be reassigned with different values
Const ensures that the variable it creates is read-only
But you can change the value of a property of a const variable
Why is it possible to .push() a new value into a const variable that points to an Array?
Because the value is still an array, and the elements of that array aren’t bound to the const of the variable that points to an array
You cannot reassign the array to another array
Array can be mutated, but we haven’t reassigned the name variable.
How should you decide on which type of declaration to use?
Use let if you intend to reassign the initialized value of the variable
Use const otherwise(?)
Stick with const until you can’t
What is the syntax for writing a template literal?
Backticks instead of quotes
For any expression you want to evaluate = ${expression}
What is “string interpolation”?
Substituting part of the string for the values of variables or expressions.
What is destructuring conceptually?
Extracting data from arrays, objects into new variables
Take pieces of complex structure, take them out and assign them to variables
What is the syntax for Object destructuring?
Declare a new object with the properties on the left side of colons, assigned to the original object variable.
How can you tell the difference between destructuring and creating Object/Array literals?
If the braces/brackets on the right side, it’s destructuring, and opposite is not.
What is the syntax for defining an arrow function?
const variable name, equals parentheses, equal sign, right arrow, curly braces
- if 1 param, parentheses optional
When an arrow function’s body is left without curly braces, what changes in its functionality?
it has to be an expression
- no if statements, etc.
How is the value of this determined within an arrow function?
In an arrow function def, you look outside to the next function definition, you keep going up and looking up, eventually hit a normal ES5 function that was called as a method of an object
Value of this is determined at call time (regular function) - to find value of object left of the dot
Value of this is determined at definition time (with arrow functions)