ES6 Flashcards
What is a code block? What are some examples of a code block?
a self contained piece of code denoted by curly braces {} if else, for, while
What does block scope mean?
variables and functions are only accessible inside the block {}
What is the scope of a variable declared with const or let?
block-scoped
What is the difference between let and const?
The variables declared by the let keyword are mutable
variables created by the const keyword are “immutable” and can’t be reassigned
Why is it possible to .push() a new value into a const variable that points to an Array?
adding to an array or object you’re not re-assigning or re-declaring the constant, it’s already declared and assigned, you’re just adding to the “list” that the constant points to.
constant is actually storing a reference to the array. When you join something into your array you are not modifying your constant value, but the array it points to.
constants makes references to the variable but not the actual value of the array.
How should you decide on which type of declaration to use?
If you don’t need to reassign the variable, use const. Otherwise use let
What is the syntax for writing a template literal?
Let variableName = String text here with ${variablename}
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?
Setting a value of an element to a new variable
taking specific fields out of an object and assigning it to a variable
What is the syntax for Object destructuring?
let { property: newVariable, property2: newVariableTwo } = objectName; or can use property as the variable name
for default value, use = defaultvalue
What is the syntax for Array destructuring?
Let [newVariable, newVariable2, newVariable3] = arrayName;
if you don’t want some of the items, use commas nothingin between
How can you tell the difference between destructuring and creating Object/Array literals?
Destructuring has the elements on the left and object/array on the right of the =
Object array literals have the elements after the = and the object/array literal name before the =
left hand side, being assigned
right hand side being evaluated
What is the syntax for defining an arrow function?
(arguments) => code block, can also use { code block }
When an arrow function’s body is left without curly braces, what changes in its functionality?
If you use an expression in the body of an arrow function, you don’t need to use the curly braces.
However, if you use a statement, you must wrap it inside a pair of curly braces as in the following example:
single expression is returned from the arrow function
How is the value of this determined within an arrow function?
an arrow function captures the this value of the enclosing context instead of creating its own this context.
determined by state of the code, lexical scope
Goes upwards to find the nearest object definition or function