ES6 Flashcards
What is a code block? What are some examples of a code block?
Code block is code that is within 1 set of curly braces
I.e. function code block, for loop code block, if statement code block
What does block scope mean?
Block scope means what variables are available within the scope of the curly braces of the code block
What is the scope of a variable declared with const or let?
The scope of let is within the code block only. It can be declare outside the code block and accessed within a code block to be changed, but outside of the code block, it cannot access whatever was inside the code block
The scope of const is block scoped also and cannot be redeclared
Var are function scoped, can be accessed outside of block scope
What is the difference between let and const?
The value of let is mutable and can be reassigned, etc
The value of const is immutable/cannot be reassigned and is a read-only reference to the value, with the exception of its property values, which can be changed and/or reassigned
Why is it possible to .push() a new value into a const variable that points to an Array?
Because the array object is not being reassigned, but the properties aka elements can be changed and thus pushed and popped off.
How should you decide on which type of declaration to use?
Depends on how you want to use the variable, if the value needs to be changed or stay the same
What is the syntax for writing a template literal?
It uses backticks (This is a template literal
) to store the string rather than single or double quotes
The variables inside the backticks would be written as ${variableName}
What is “string interpolation”?
Template literals can have substitutions within the backticks which embed variables and expressions in strings that JavaScript automatically replaces with their values without having to concatenate
I.e. const firstName = ‘John’,
const lastName = ‘Doe’;
const greeting = `Hi ${firstName}, ${lastName}`; console.log(greeting); // Hi John, Doe
What is destructuring, conceptually?
Getting multiple variables from array or object elements and assigning them to another variable.
What is the syntax for Object destructuring?
There’s a variable in front like const, var, let and curly braces and properties you want to assign, end curly brace, = assignment operator and the object name
const { propertyName: value, propertyName: value, propertyName: value, } = objectName;
Don’t need the colon and value name if the variable is the same name as the value name
What is the syntax for Array destructuring?
There’s a variable in front like const, var, let and square brackets and name of variable to be assigned at the location of the array, square bracket = assignment operator and the array name
const [book3, book4, book5] = library;
Can you comma to skip the order of the array to get to another spot
I.e. const [, , , book6] = library;
How can you tell the difference between destructuring and creating Object/Array literals?
Destructuring has curly and square brackets on the left side of the = assignment operator
Creating has curly and square brackets on the right side of the = assignment operator
What is the syntax for defining an arrow function?
Const variableName = (parameter1, parameter2) => {expression with the parameters};
Parameter1 => {normal code block} or return expression
*no parameter () =>
If no parameter, parentheses, if one parameter, parentheses optional, if multiple parameters, parentheses needed
(… args) => expression to define an arrow function
(…args) => { statements} to define an arrow function with multiple statements
When an arrow function’s body is left without curly braces, what changes in its functionality?
You don’t have to type return for the return expression, it will implicitly return the expression
Also, JavaScript can’t tell the difference between the expression in the block and object if there’s an object in the expression
How is the value of this determined within an arrow function?
The value of this is defined by the surrounding code block
The arrow function captures the this value of the enclosing context instead of “creating” its own this context if the arrow function were not there
The value of this is defined at definition time during arrow functions