ES6 Flashcards
What is a code block? What are some examples of a code block?
A block of code that contains statements that you want to execute together contained within curly braces {}
Function, conditional, loop code block
What does block scope mean?
Can’t access variables within the block, outside of the block
What is the scope of a variable declared withconstorlet?
Block-scoped
What is the difference betweenletandconst?
Let: mutable, not initialized to any value, and not attached to the global object as a property
Const: read-only reference, can’t be reassigned (immutable), need to initialize the value to the variable declared, constant identifiers are in uppercase
Why is it possible to.push()a new value into aconstvariable that points to anArray?
An array is a special type of object and the property of aconstobject can be change but it cannot be change to reference to thenewobject
Not changing the variable name binding, mutating the content of the array which are mutable
How should you decide on which type of declaration to use?
Use const until you need to reassign the value of the variable, then use let
What is the syntax for writing a template literal?
String must be wrapped with backticks ( string
)
Embedded JavaScript expressions wrapped with ‘$’ and ‘{}’, ${expression}
If string contains a backpack, it must be escaped using a backslash ( \ )
Allows for multi-line strings, string interpolations with embedded expressions, and tagged templates
What is “string interpolation”?
Insert, or interpolate, variables into strings using template literals. Use placeholders of the form ${expression} to perform substitutions for embedded expressions
What is destructuring, conceptually?
Unpack values from arrays, or properties from objects, into distinct variables
What is the syntax forObjectdestructuring?
const { var1, var2: varAlias2 } = obj
Use (:) colon to rename variable
What is the syntax forArraydestructuring?
const [var1, var2, var3] = arr
Use (,) to skip elements in array
How can you tell the difference between destructuring and creatingObject/Arrayliterals?
When destructing, the curly or square brackets appear on the left of the assignment operator, however when creating object or array literals the brackets appear on the right
What is the syntax for defining an arrow function?
(param1, param2) => {
return;
};
param => exp; // implicit return
() => exp;
When an arrow function’s body is left without curly braces, what changes in its functionality?
Function body evaluated as an expression and implicit return
How is the value ofthisdetermined within an arrow function?
Arrow functions don’t have their own bindings to this; have lexical scoping to this context. Takes this from the enclosing function, value of this it determined when the function is defined.
(Versus ES5 function definition, value of this is determined when the function is called.)