ES6 Flashcards
What is a code block? What are some examples of a code block?
Code surrounded by curly brackets. If statements, for loops, function definitions
What does block scope mean?
Exists within curly braces {}
The scope is based on the container code block
What is the scope of a variable declared with ‘const’ or ‘let’?
block-scoped
What is the difference between ‘let’ and ‘const’?
Let values can be resigned while const can’t be resigned
const is a ‘read-only’ reference to a value
Why is it possible to ‘.push()’ a new value into a ‘const’ variable that points to an ‘Array’?
Objects are mutable, we are not reassigning the const variable, just updating the array
The value of ‘const’ variable is mutable; you can change the value of the property.
How should you decide on which type of declaration to use?
If variable is reassigned, use let.
if variable doesn’t reassigned, use const
What is the syntax for writing a template literal?
Use backticks `
Backticks beginning and end of template literal; ${} to embed in literal “${expresstion}”
What is “string interpolation”?
Substitute result of expressions into a string; using ${} syntax
Replace variables and expressions in a string through substitution in template literals using a special block.
What is destructuring, conceptually?
Assigning properties of an object to a variable (different or same name).
What is the syntax for ‘Object’ destructuring?
let/const/var {
property1: variable1,
property2: variable2
} = object;
What is the syntax for ‘Array’ destructuring?
let/const/var [variable1, variable2] = array;
How can you tell the difference between destructuring and creating ‘Object/Array’ literals?
Brackets on left hand side of ‘=’ for destructuring; creating will be on right hand side
What is the syntax for defining an arrow function?
let add = (x,y) => x + y; let add = (x, y) => { return x + y; };
Use parenthesis for parameters if 0 and more than 1 parameters. 1 parameter does not need it; use curly braces if 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 need a return statement
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
How is the value of ‘this’ determined within an arrow function?
Arrow function, ‘this’ is defined at definition time, not call time.
‘this’ goes to global scope or first non arrow function
arrow function captures the ‘this’ value of the enclosing context instead of creating its own this context.